gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 12:36:50 25 4
gpt4 key购买 nike

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

关于java - Selenium WebDriver 无法通过链接文本找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20380720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com