我正在尝试选择一个包含一个锚点的元素,但文本被埋在一个div内的段落中.这是我正在使用的
HTML:
<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>
我使用的代码试图阻止它是针对“Smoke Sequential”文本:
driver.findElement(By.linkText(service)).click();
变量“service”持有“Smoke Sequential”的位置.当我运行它,我得到以下错误:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Smoke Sequential"}
任何帮助将不胜感激.
解决方法
问题可能在html的其余部分,您没有发布的部分.
<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>
我能找到的元素没有麻烦:
driver.findElement(By.linkText("Smoke Sequential")).click();
如果元素中有更多文本,可以尝试通过部分链接文本查找:
driver.findElement(By.partialLinkText("Sequential")).click();