How to get the text attribute of a input element? - webdriver - java

<ul class="list-group opened-list d-none" xpath="1">
<li class="list-group-item col-12" xpath="1">My team</li>
<li class="list-group-item col-12" xpath="1">My name</li>
<li class="list-group-item col-12" xpath="1">My film</li>
<li class="list-group-item col-12" xpath="1">My football teammate</li>
</ul>
Dropdown list without select tag

To get all li elements use .list-group.opened-list .list-group-item css selector.
Code below wait visibility of li elements and then print text for each:
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> options = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector(".list-group.opened-list .list-group-item")));
options.forEach(element -> System.out.println(element.getText()));
If you want to select one of the element by text, check example here.

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

I want to extract all <li> element text that are under <ul>

I need to click on all elements BASIC, TRACKS, ...
My idea is to extract all elements in list then using list count and loop, I'll click on each and every element.
Need to check that each and every element is working even if new element is added I don't want to check code.
<div class="headerarea" style="" xpath="1">
<h2>
<span id="ctl00_ctl00_phDesktop_lblModuleTitle">Abstract Setup</span>
</h2>
<ul>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl01_btnModuleNavigation" class="headerarea_active" href="https://staging.m-anage.com/Modules/Abstract/Setup/basics.aspx">Basic</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl02_btnModuleNavigation" href="https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Tracks">Tracks</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl03_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/steps.aspx">WIZARD</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl04_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/keywords.aspx">KEYWORDS</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl05_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/categories.aspx">CATEGORIES</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl06_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/conditions.aspx">CONDITIONS</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl07_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/interests.aspx">Interests</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl08_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/templates.aspx">Templates</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl09_btnModuleNavigation" href="https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Index">Submission fee</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl10_btnModuleNavigation" href="https://staging.m-anage.com/testselenium/en-US/Mail/MailServerSetup/Index?pModuleType=Abstract" style="">SMTP Setup</a></li>
<li>
<a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl11_btnModuleNavigation" href="https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Coauthor">Co-author</a></li>
</ul>
</div>
I tried travelling to child path but no success
Here is the java code that I tried.
List<WebElement> tags =
driver.findElements(By.xpath("//div[#class='headerarea']/ul/li"));
for(int i=0;i<tags.size();i++) {
while(???) {
//driver.findElement(By.xpath("//div[#class='headerarea']/ul/li")).click();
}
}
Try below code :
List<WebElement> links = driver.findElements(By.tagName("li"));
for (int i = 1; i < links.size(); i++)
{
System.out.println(links.get(i).getText());
}
You can also use WebDriverWait if you are facing synchronization issue.
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> links = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName(li)));
for (int i = 1; i < links.size(); i++)
{
System.out.println(links.get(i).getText());
}
List<WebElement> tags = driver.findElements(By.cssSelector(".headerarea ul>li"));
for(WebElement e : tags) {
e.click();
}

How do I read the element from a dropdown menu of a html?

I am parsing the following html by Selenium FirefoxDriver -
<div id="primaryNav" style='background: url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/primaryNavBackground._V161555288_.gif") repeat-x bottom;'>
<div id="menuh">
<ul>
<li style="visibility:hidden;
height:24px"/>
<li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
▼</span>
<div class="parent">
<div class="dropdownlinks">
<div class="subitem">Product Links</div><div class="subitem">Banner Links</div><div class="subitem">Link to Any Page</div><div class="subitem">Link Checker</div>
</div>
</div>
</li>
<li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
<a href="http://widgets.amazon.in/?_encoding=UTF8&store=httpswwwvanta-21&tag=httpswwwvanta-21" style='float:left;background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-left._V161554471_.gif") no-repeat left top;'>Widgets</a>
</li>
<li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
<a href="/gp/advertising/api/detail/main.html" style='float:left;background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-left._V161554471_.gif") no-repeat left top;'>Product Advertising API</a>
</li>
<li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
▼</span>
<div class="parent"><div class="dropdownlinks">
<div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=earningsReport" >Earnings Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=ordersReport" >Orders Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=linkTypeReport" >Link-Type Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=trendsReport" >Daily Trends</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=tagsReport" >Tracking ID Summary Report</a>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
I am trying to select the "Earnings Report" from the dropdown menu.
I tried like this --
dropDownButton: WebElement = driver.findElement(By.xpath(".//a[#href='https://affiliate-program.amazon.in/gp/associates/network/reports/report.html?ie=UTF8&reportType=earningsReport']"))
dropDownButton.click()
I also tried like this --
val dropDownButton = driver.findElement(By.linkText("Earnings Report"))
dropDownButton.click()
In both the cases, The code runs only when I hover my mouse over the dropdown menu. No manual click is required.
I also tried the following code which I am not sure if correct -
import scala.collection.JavaConversions._
def selectValueFromDropdown( value: String) = {
var options = driver.findElements(By.id("menuh"));
for(option <- options) {
if (value.equals(option.getText())) {
option.click()
}
}
}
selectValueFromDropdown("Earnings Report")
I am kinda lost here. Please suggest a solution in either Java or Scala.
EDIT: I get to this page after log-in from the main page. Can that be a problem?
Please try this:
First select the drop down and then select by value or Index-
Select drpdown = new Select(driver.findElement(By.xpath("Locator of the dropdown"));
drpdown.SelectByValue("Earning Report");
If "Earning Report" is a visible Text then-
drpdown.selectByVisibleText("Earning Report");
As you mentioned that you have to hover your mouse over the dropdown menu for it to work. Your menu also has sub-menus. So, before clicking the link you need to use the "perform" method of "Actions". In this manner it allows Selenium to spot a particular sub-menu while holding the menu. The code for that is:
val menuElement = driver.findElement(By.id("menuh"))
/* If the css selector used below does not match the element that
* fires the hover action then check which element fires it and update
* the selector */
val subMenuElement = driver.findElement(By.cssSelector("#menuh li:nth-child(5)"))
val earningsReportElement = driver.findElement(By.linkText("Earnings Report"))
val action = new Actions(driver)
action.moveToElement(menuElement).perform()
action.moveToElement(subMenuElement).perform()
action.moveToElement(earningsReportElement)
action.click()
action.perform()
try this-
driver.findElement(
By.xpath("//a[contains(#href,'earningsReport')]"))
.click();
The solution given by #Dagojvg worked after re-arranging the expressions.
import org.openqa.selenium.interactions.Actions
val action = new Actions(driver)
val menuElement = driver.findElement(By.id("menuh"))
action.moveToElement(menuElement).perform()
val subMenuElement = driver.findElement(By.cssSelector("#menuh li:nth-child(5)"))
action.moveToElement(subMenuElement).perform()
val earningsReportElement = driver.findElement(By.linkText("Earnings Report"))
action.moveToElement(earningsReportElement)
action.click()
action.perform()

Iterate through a list?

I have the following markup.
Edit: Added full markup
<div id="SelectList">
<div class="select-area-left"></div>
<div class="select-area-right"></div>
<div id="SelectedOption">Option0</div>
<ul id="ShowOptions">
<li id="ShowOption0">Option0</li>
<li id="ShowOption1">Option1</li>
<li id="ShowOption2">Option2</li>
<li id="ShowOption3">Option3</li>
<li id="ShowOption4">Option4</li>
<li id="ShowOption5">Option5</li>
<li id="ShowOption6">Option6</li>
<li id="ShowOption7">Option7</li>
<li id="ShowOption8">Option8</li>
<li id="ShowOption9">Option9</li>
<li id="ShowOption10">Option10</li>
<li id="ShowOption11">Option11</li>
<li id="ShowOption12">Option12</li></ul></div>
And i'm trying to use the following code to print out each list elements text.
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='SelectList']/ul"));
for (WebElement element: allElements) {
System.out.println(element.getText());
}
But its just given a blank output. Is there something i'm missing?
Try following code :
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='SelectList']/ul/li"));
for (WebElement element: allElements) {
System.out.println(element.getText());
}
Basically you missed to point li element in the xpath used. Above code should work for you.

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