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.
任何帮助将不胜感激。
5 个解决方案
#1
22
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();
#2
4
Use xpath and text()
使用xpath和text()
driver.findElement(By.Xpath("//strong[contains(text(),'" + service +"')]"));
#3
#4
0
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。
#5
0
find_elements_by_xpath("//*[@class='class name']")
is a great solution
是一个很好的解决方案
#1
22
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();
#2
4
Use xpath and text()
使用xpath和text()
driver.findElement(By.Xpath("//strong[contains(text(),'" + service +"')]"));
#3
0
This doesn't seem to have <a> </a>
tags so selenium might not be able to detect it as a link.
这似乎没有 标签,因此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 .
如果这是该页面中唯一具有此类的元素。
#4
0
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。
#5
0
find_elements_by_xpath("//*[@class='class name']")
is a great solution
是一个很好的解决方案