小编典典

Selenium WebDriver无法通过链接文本找到元素

selenium

我正在尝试选择一个包含锚点的元素,但文本被埋在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();

变量“服务”中包含“烟雾顺序”的位置。运行它时,出现以下错误:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Smoke Sequential"}

任何帮助将不胜感激。


阅读 378

收藏
2020-06-26

共1个答案

小编典典

问题可能出在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();
2020-06-26