Selenium WebDriver无法按链接文本查找元素 [英] Selenium WebDriver can't find element by link text
问题描述
我正在尝试选择包含锚点的元素,但文本隐藏在div内部的段落中。这是我正在使用的HTML:
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>
我用来试图阻止它的代码是针对Smoke Sequential文本:
The code I'm using to try to snag it is targeting the "Smoke Sequential" text with:
driver.findElement(By.linkText(service)).click();
变量'service'中包含Smoke Sequential。当我运行它时,我收到以下错误:
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.
推荐答案
问题可能出在html的其余部分,即你没有发布的部分。
The problem might be in the rest of the html, the part that you didn't post.
通过这个例子(我刚关闭了打开的标签):
With this example (I just closed the open tags):
<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>
我能够毫无困难地找到元素:
I was able to find the element without trouble with:
driver.findElement(By.linkText("Smoke Sequential")).click();
如果元素中有更多文本,您可以通过部分链接文本尝试查找:
If there is more text inside the element, you could try a find by partial link text:
driver.findElement(By.partialLinkText("Sequential")).click();
这篇关于Selenium WebDriver无法按链接文本查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!