Reputation: 5799
I have the below HTML code. I want to get all the text within the hyperlink tag.
<div ng-model='sCur'>
<ul sa-toggle-menu="" role="menu" class="sa-dropdown-menu">
<li><a ng-bind="item[lebel]" ng-click="itemSelect(item)" class="ng-binding">1</a>
<li><a ng-bind="item[lebel]" ng-click="itemSelect(item)" class="ng-binding">2</a>
<li><a ng-bind="item[lebel]" ng-click="itemSelect(item)" class="ng-binding">3</a>
</ul>
</div>
I am trying the below java code. But I get empty text values.
List<WebElement> currencies = driver.findElements(By.cssSelector("div[ng-model='sCur'] ul li"));
for (WebElement option : currencies) {
System.out.println("Text :" + option.getText());
}
Upvotes: 0
Views: 5695
Reputation: 61
WebDriver driver=new FirefoxDriver();
driver.get("http://www.naukri.com/");
// Click on dropdown
driver.findElement(By.xpath(".//[@id='exp_dd']/div[1]/span")).click();
//Capture all dropdown element in a List
java.util.List<WebElement> elements =driver.findElements(By.xpath(".//*[@id='exp_dd']/div[2]//ul/li"));
for(WebElement ele:elements){
String str=ele.getText();
if(str.equalsIgnoreCase("8"))
ele.click();
}
Upvotes: 0
Reputation: 5799
Ok.. Here was the issue.
As the data is bound to the element on runtime, the list was not present in the DOM. Only when the drop down arrow is clicked, the elements are added to the DOM.
After triggering the combobox click, all the elements became valid and getText() worked.
driver.findElements(By.cssSelector("div[ng-model='sCur'] ul")).click();
List<WebElement> currencies = driver.findElements(By.cssSelector("div[ng-model='sCur'] ul li a"));
for (WebElement option : currencies) {
System.out.println("Text :" + option.getText());
}
Upvotes: 1
Reputation: 5080
Try this:
List<WebElement> currencies = driver.findElements(By.className("ng-binding"));
for (WebElement option : currencies) {
System.out.println("Text :" + option.getAttribute("ng-bind"));
System.out.println("Text :" + option.getText());
}
Upvotes: 0
Reputation: 22720
Because you are selecting li
elements and not a
elements. Use
By.cssSelector("div[ng-model='sCur'] ul li a")
Upvotes: 0