gpt4 book ai didi

java - 无法使用 Java 和 Selenium 通过部分链接文本查找元素

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:43 24 4
gpt4 key购买 nike

使用 geckodriver 0.23.0、firefox 64.0.2、selenium 3.12、java 8 我无法通过部分链接文本找到元素。不使用框架。链接文本是“Accounts (1)”。 “查看所有帐户”页面上只有一个相同文本的其他实例

html:

<li>    
<a href="/accounting/view_all_accounts?_t=039f18daf35b4a00f0093dd17aa70730be385f6f&amp;to_render=account" class="first accounting_page_menu ">Accounts (1)</a>
<ul>
<li>
<a href="/accounting/details?_t=e3d4ea94f5ed862d95196a620f1147be13b02979&amp;to_render=account" class="first accounting_page_menu ">Primary</a>
</li>

<li>
<a onclick="javascript: ModalUtil.loadEditableModal('/accounting/details_new_account', false, false, true);" class="add-accounts">Add New Account...</a>
</li>
<li>
<a href="/accounting/view_all_accounts?_t=039f18daf35b4a00f0093dd17aa70730be385f6f&amp;to_render=account" class="first accounting_page_menu ">View All Accounts</a>
</li>
</ul>
</li>

我用来查找元素的代码:“Accounts (n)”,其中 n = 1、2、3 ...

driver.findElement(By.partialLinkText("Accounts (")).click();

我尝试使用“Accounts”和“Accounts (”,它们都返回相同的 404 not found - no such element error

控制台日志:

1547499923019   webdriver::server   DEBUG   -> POST /session/bed7e7d2-d849-4bd0-ab17-fdca3fb080f9/element {
"value": "Accounts ",
"using": "partial link text"
}
1547499923020 Marionette TRACE 0 -> [0,315,"WebDriver:FindElement",{"using":"partial link text","value":"Accounts "}]
1547499923241 Marionette TRACE 0 <- [1,315,{"error":"no such element","message":"Unable to locate element: Accounts ","stacktrace":"WebDriverError@chrome://mario ... entError@chrome://marionette/content/error.js:388:5\nelement.find/</<@chrome://marionette/content/element.js:339:16\n"},null]
1547499923240 webdriver::server DEBUG <- 404 Not Found {"value":{"error":"no such element","message":"Unable to locate element: Accounts ","stacktrace":"WebDriverError@chrome://marionette/content/error.js:178:5\nNoSuchElementError@chrome://marionette/content/error.js:388:5\nelement.find/</<@chrome://marionette/content/element.js:339:16\n"}}

最佳答案

正如您提到的,您正试图找到文本为 Accounts (n) 的元素,其中 n = 1, 2, 3 ... 以及更多带有 linkText< 的元素/em> 因为添加新帐户查看所有帐户 存在,而不是使用 partialLinkText 最好使用 XPath 并且您可以使用以下解决方案:

  • XPath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li/a[@class='first accounting_page_menu' and starts-with(@href,'/accounting/view_all_accounts?')][starts-with(.,'Accounts')]"))).click();

更新

根据讨论 Official locator strategies for the webdriver 部分链接文本选择器优于XPath选择器。然而,根据此用例,由于存在类似的链接文本,构建XPath 会更容易。

关于java - 无法使用 Java 和 Selenium 通过部分链接文本查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203338/

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