Java – selenium webdriver cannot find element through link text
•
Java
I'm trying to select an element that contains an anchor, but the text is buried in a paragraph within a Div This is the HTML I'm using:
<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 used to try to block it is for "smoke sequential" text:
driver.findElement(By.linkText(service)).click();
The variable "service" holds the position of "smoke sequential" 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 appreciated
Solution
The problem may be in the rest of the HTML that you did not publish
With this example (I just closed the open tab):
<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> </div> </div> </a>
The elements I can find are not troublesome:
driver.findElement(By.linkText("Smoke Sequential")).click();
If there is more text in the element, you can try to find it through some linked text:
driver.findElement(By.partialLinkText("Sequential")).click();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码