How get all li below div content selenium - java

How can i get all <a> value?
<div id="mCSB_1_container" class="mCSB_container" style="position: relative; top: -985px; left: 0px;" dir="ltr">
<li>
All countries
</li>
<li>
Germany
</li>
<div>
When i use
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='mCSB_1_container']/li/a"));
for (WebElement element: allElements) {
System.out.println(element.getText());
}
i get empty values.

Try one of below selectors:
By.xpath("//div[#id='mCSB_1_container']/li/a")
By.cssSelector("#mCSB_1_container li a")
By.cssSelector("div[id='mCSB_1_container'] li a")
By.xpath("//*[contains(#id, 'mCSB_1_container')]/li/a")
Or one of this findElements (I don't know if Java contains FindElements extension to IWebElement, but it work in other languages):
List<WebElement> allElements = driver
.findElement(By.id("mCSB_1_container"))
.findElements(By.cssSelector("li a"));
List<WebElement> allElements = driver
.findElement(By.id("mCSB_1_container"))
.findElements(By.tagName("a"));
List<WebElement> allElements = driver
.findElement(By.id("mCSB_1_container"))
.findElements(By.xpath("//li/a"));
Also, if no one works, try to find when breaks, using something like:
By.cssSelector("#mCSB_1_container") //Shoud find the id.
By.cssSelector("#mCSB_1_container li") //Shoud find the li inside the id.
If is unable to find one of they, there is some problem with frames or wait the element display

List<WebElement> allElements = driver.findElements(By.tagName("a"));
for(WebElement element: allElements)
{
System.out.println(element.getText());
}

Jsoup is a great html parser and it can be used with Selenium.
here is the Jsoup web page; https://jsoup.org/
and if you want to get all links from page you can use Jsoup like that;
Document doc = Jsoup.connect("https://jsoup.org/").get();
Elements links = doc.select("a[href]");
for (Element link : links) {
print(link);
}
this code snippet allows you to get all href values of all links. if you want to get all from web page use ;
Elements links = doc.select("a");
instead of
Elements links = doc.select("a[href]");

Related

How to identify the text within the <a> using Selenium and Java

This is the code of the demo store I want to test:
<li class="level0 nav-2 parent">
<a href="http://demo-store.seleniumacademy.com/men.html"
class="level0 has-children">Men</a>
<ul class="level0">
<li class="level1 view-all">
<a class="level1" href="http://demo-
store.seleniumacademy.com/men.html">View All
Men</a>
</li>
<li class="level1 nav-2-1 first"><a></a></li>
<li class="level1 nav-2-2"><a href="http://demo-
store.seleniumacademy.com/men/shirts.html"
class="level1">text to get</a>
</li>
<li class="level1 nav-2-3"><a></a></li>
</ul>
</li>
I want to get text of those subcategories so I can later click on specific category by using text inside of a link element. My code is:
public Subcategory openSubcategory (String subcategoryName){
List<WebElement> subcategories = driver.findElements(By.cssSelector("a.level1"));
for (WebElement element: subcategories) {
if (element.getText().equals(subcategoryName)){
element.click();
break;
}
}
return new Subcategory(driver);
}
But it won't go inside loop, probably because element.getText() is empty.
To click on the WebElement with text as Shirts first you have to Mouse Hover the element with text as Men inducing WebDriverWait for the visibilityOfElementLocated and you can use the following locator strategies:
public Subcategory openSubcategory (String subcategoryName){
WebElement menuMen = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(#class,'has-children') and text()='Men']")));
new Actions(driver).moveToElement(menuMen).build().perform();
List<WebElement> subcategories = driver.findElements(By.xpath("//a[contains(#class,'has-children') and text()='Men']//following::ul[1]//li/a"));
for (WebElement element: subcategories) {
if (element.getText().equals(subcategoryName)){
element.click();
break;
}
}
return new Subcategory(driver);
}
element.getText() can be empty.
Try:
element.getAttribute("value");
// or
element.getAttribute("innerHTML");
Change your CSS to be:
"a[class*='level']";
// or
"a[]";
Debug the code and check if the element is not null

Using Selenium in Java to get table contents

This is a continuation from my other post
Using JSoup to get data-code value of a table
I am trying to get the text inside the <span> tags on the table using the cssSelector() method in Selenium webdriver
<table class ="team-list">
<tr data-code="1">
<td>
<span>
Get This Text
</span>
</td>
</tr>
</table>
I have tried the following code, but this will print out the text in all cells for each row, but i only need to get the one inside the <span> tags
WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");
List<WebElement> elements = driver.findElements(By.cssSelector("table.team-list td"));
for(WebElement element: elements)
{
System.out.println(element.getText());
}
if you know the text you are looking for you can the following:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");
List<WebElement> elements = driver.findElements(By.cssSelector("table.team-list td"));
for(WebElement element: elements)
{
if(element.getText().equals("Get This Text"))
System.out.println(element.getText());
}
That's might be o(n) but if you are not care about performance that could solve your problem.
I think you should change your selector to this:
By.cssSelector("table.team-list span")
Try something like this.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");
List<WebElement> elements = driver.findElements(By.cssSelector("table.team-list td"));
for(WebElement element: elements)
{
try{
System.out.println(element.findElement(By.tagName("span")).getText());
}(org.openqa.selenium.NoSuchElementException nsee){
}
}

HtmlUnit get div element by class inside a DomElement?

Hello I am using HtmlUnit library and I need to get some href attribute from an a tag, inside some div:
<div class="threadpostedin td alt">
<p>Forum:<br>
<a href="programming/website-development/"
title="Website Development">Website
Development</a></p>
</div>
This div is located inside a <li> which is located inside a <ol>
to get the ol I did this:
HtmlOrderedList l = (HtmlOrderedList) this.page.getElementById("searchbits");
The html:
<ol class="searchbits" id="searchbits" start="1">
Now from the div I posted, I need to get the href "programming/website-development/", but I am not sure how to do this. Yes the div has a class name, but if I do
for (DomElement ele : l.getChildElements()) {
System.out.println(ele.getByXPath("//div[#class='threadpostedin td alt']").size());
break;
}
it will print 15, because overall there are 15 lists in the ol, in each list there is one div with class threadpostedin td alt. What I need to do, is the the exact div with class threadpostedin td alt in the DomElement I got from the iteration, and not get the list of all divs with that class.
Is there a way to do this with HtmlUnit?
I assume you have more links than one to make it more detailed.
HtmlElement element = page.getByXPath("//div[#class='threadpostedin td alt']").get(0);
DomNodeList<DomNode> nodes = element.querySelectorAll("a");
for(DomNode a : nodes) {
if(a.getAttributes().getNamedItem("href") !=null) {
String href = page.getFullyQualifiedUrl(a.getAttributes().getNamedItem("href").getNodeValue()).toString().toLowerCase();
String baseUrl = page.getBaseURL().toString();
}
}

Selenium: Select child element with no parameters

due to the fact i'm using selenium for the first time i have a questin on selecting a child element without parameters.
I'm trying to get the child-div "element to be clicked" to perform a click.
Java:
WebElement element = driver.findElement(By.className("parent"));
WebElement element2 = element.findElement(By.xpath("/div/div/div")); // should be wrong
element2.click();
Given HTML-Code:
<div class="parent">
<div>
<div>
<div>element to be clicked</div>
</div>
</div>
</div>
You can use
element.findElement(By.xpath("//div[text()='element to be clicked']"));
With RegEx
element.findElement(By.xpath("//div[matches(text(),'RegExExpression']"));
While there's nothing to identify directly, we can do something like this..
List<WebElement> divs = driver.findElements(By.tagname("div")); //This will return all div elements
Then we can try something unique to that div..
for(int i = 0; i < divs.size(); i++) {
if(divs.get(i).getText().equals("element to be clicked")) {
divs.get(i).click();
break;
}
}

Getting child elements using WebDriver

I've got the following HTML code:
<div class="ui-selectmenu-menu" style="z-index: 1; top: 251px; left: 37px;">
<ul class="ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom" aria-hidden="true" role="listbox" aria-labelledby="gwt-uid-191-button" id="gwt-uid-191-menu" style="width: 270px; height: auto;" aria-disabled="false" aria-activedescendant="ui-selectmenu-item-999">
<li role="presentation" class="ui-selectmenu-item-selected">
All Applications</li>
<li role="presentation" class="">
Option Alpha</li>
<li role="presentation" class="ui-corner-bottom">
Option Beta</li>
</ul>
</div>
...
<div class="ui-selectmenu-menu"...>...</div>
I'm able to get the WebElement for ui-selectmenu-menu like this (there are many on the page; hence, the use of findElements) :
List<WebElement> dropdowns = driver.findElements(By.className("ui-selectmenu-menu"));
And the ul below it like this:
WebElement ddChild = dropdowns.get(0).findElement(By.className("ui-selectmenu-menu-dropdown"));
I'm even able to grab all the li under the ddChild like this:
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li[*]"));
But the problem that I can't seem to figure out how to grab the text-value of the <a href="#nogo"... tag under each li element.
I'd like to be able to loop through all the ddOpts and grab the <a href="#nogo"... text values and save them to an ArrayList<String>.
So, for example, my first ArrayList<String> value would contain All Applications, then Option Alpha, then Option Beta, and then jump to the next ul element from the next dropdowns and do the whole process again, all while adding to the ArrayList<String>.
I'm sure its a simple solution but I've got limited experience with Selenium WebDriver.
Thanks!
PS: Is there a simple way to grab the child of a WebElement?
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
links.add(we.getText();
}
To extract the href attribute of the WebElement (referring to the anchor tag <a> in this example, do this:
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
// ADD all the href attribute strings to the list
links.add(we.getAttribute("href"));
}
This may also solve your problem:
List<WebElement> dropdowns = driver.findElements(By.className("x-combo-list"));
WebElement ddChild = dropdowns.get(0).findElement(By.className("x-combo-list-inner"));
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id=\"x-auto-98\"]/div[4]"));
for(WebElement we:ddOpts){
System.out.println(we.getText());
if(we.getText().contains("ROLE_MANAGER")){
we.sendKeys("ROLE_MANAGER");
we.click();
break;
}
}
the below code will select the OptionAlpha in the dropdown of the above HTML code
driver.findElement(By.xpath("//*[#class='ui-selectmenu-menu')).click();
driver.findElement(By.xpath("//*[#class='ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom']//**[text()='Option Alpha']")).click();
Please try the below code to get all the links in the <a href
List<WebElement> allLis = driver.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a");
// Looping through above list using for-each loop
for(WebElement eachLi : allLis) {
System.out.println(eachLi.getText());
}
Hope this helps.
href="#nogo" is same for all the anchor tags, so it might create ambiguity in selecting the item by the method
dropdowns.findelement(By.linktext("#nogo"));

Categories

Resources