Java Selenium WebDriver 无法通过链接文本找到元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20380720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium WebDriver can't find element by link text
提问by Taelus
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:
我正在尝试选择一个包含锚点的元素,但文本隐藏在 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>
The code I'm using to try to snag it is targeting the "Smoke Sequential" text with:
我用来尝试阻止它的代码是针对“Smoke Sequential”文本的:
driver.findElement(By.linkText(service)).click();
Where the variable 'service' holds "Smoke Sequential" in it. When I run it, I get the following error:
变量“service”在其中包含“Smoke Sequential”。当我运行它时,出现以下错误:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Smoke Sequential"}
Any help would be greatly appreciated.
任何帮助将不胜感激。
采纳答案by Marielle
The problem might be in the rest of the html, the part that you didn't post.
问题可能出在 html 的其余部分,即您没有发布的部分。
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();
回答by user2062360
This doesn't seem to have <a> </a>
tags so selenium might not be able to detect it as a link.
这似乎没有 <a> </a>
标签,因此 selenium 可能无法将其检测为链接。
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 .
如果这是该页面中具有此类的唯一元素。
回答by marchocolate
A CSS selector approach could definitely work here. Try:
CSS 选择器方法绝对可以在这里工作。尝试:
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.
如果在这个类项目之前还有其他锚点,这将不起作用。如果您执行诸如“#my_table > a.item”之类的操作,您可以更好地指定确切的元素,其中 my_table 是锚点是其子项的表的 id。
回答by user2932876
Use xpath and text()
使用 xpath 和 text()
driver.findElement(By.Xpath("//strong[contains(text(),'" + service +"')]"));
回答by Jean Fran?ois
find_elements_by_xpath("//*[@class='class name']")
is a great solution
是一个很好的解决方案