I'm trying to select an element that includes an anchor, but the text is buried in a paragraph inside of a div. Here's the HTML I'm working with:
<a class="item" ng-href="#/catalog/90d9650a36988e5d0136988f03ab000f/category/DATABASE_SERVERS/service/90cefc7a42b3d4df0142b52466810026" href="#/catalog/90d9650a36988e5d0136988f03ab000f/category/DATABASE_SERVERS/service/90cefc7a42b3d4df0142b52466810026">
<div class="col-lg-2 col-sm-3 col-xs-4 item-list-image">
<img ng-src="csa/images/library/Service_Design.png" src="csa/images/library/Service_Design.png">
</div>
<div class="col-lg-8 col-sm-9 col-xs-8">
<div class="col-xs-12">
<p>
<strong class="ng-binding">Smoke Sequential</strong>
</p>
The code I'm using to try to snag it is targeting the "Smoke Sequential" text with:
driver.findElement(By.linkText(service)).click();
Where the variable 'service' holds "Smoke Sequential" in it. When I run it, I get the following error:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Smoke Sequential"}
Any help would be greatly appreciated.
This question is related to
java
html
selenium
selenium-webdriver
find_elements_by_xpath("//*[@class='class name']")
is a great solution
Use xpath and text()
driver.findElement(By.Xpath("//strong[contains(text(),'" + service +"')]"));
A CSS selector approach could definitely work here. Try:
driver.findElement(By.CssSelector("a.item")).Click();
This will not work if there are other anchors before this one of the class item. You can better specify the exact element if you do something like "#my_table > a.item" where my_table is the id of a table that the anchor is a child of.
This doesn't seem to have <a> </a>
tags so selenium might not be able to detect it as a link.
You may try and use
driver.findElement(By.xpath("//*[@class='ng-binding']")).click();
if this is the only element in that page with this class .
Source: Stackoverflow.com