【问题标题】:Not able to find element by partial link text using Java and Selenium无法使用 Java 和 Selenium 通过部分链接文本找到元素
【发布时间】:2019-06-09 17:30:32
【问题描述】:

使用 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 - 没有这样的元素错误

控制台日志:

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"}}

【问题讨论】:

  • 如果无法通过部分链接文本查找“帐户”,则说明有问题。您是否尝试过添加等待?你确定没有 IFRAME 吗?你确定大写吗?
  • 尝试在开发控制台中运行$x("//a[.='Accounts (1)']")。它找到什么了吗?

标签: java selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

正如您所提到的,您正在尝试查找文本为 Accounts (n) 的元素,其中 n = 1、2、3 ... 以及更多带有 linkText 因为 Add New AccountView All Accounts 存在,而不是使用 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 选择器。然而,根据这个 usecase,由于存在类似的 link texts,因此构造 XPath 会更容易。

【讨论】:

  • 好答案...但是使用 XPath 比使用 partialLinkText 更好吗?
  • @MosheSlavin 添加了对答案的更新。让我知道这是否能回答您的反问。
  • 我看过这个讨论,因此我的回答和评论......我必须说我向你学习了很多!谢谢
  • 关于 partialLinkText 让我感到困惑的是无法找到唯一的名称:---- 添加新帐户 - 末尾没有 s -> 不应通过搜索“帐户”或“帐户(“ ---- 查看新帐户 - 末尾没有空间 -> 不应通过搜索“帐户”或“帐户(” ---- 我在这里遗漏了什么吗?
  • @Ivana 当您的目标文本是Accounts (n),定位部分文本Accounts 可能包括Accounts (1)View All Accounts 两者。另一方面,我会强烈避免Accounts (,因为( 会在Locator Strategy 中引起很多混乱。因此我会推荐 xpathcssSelector.
猜你喜欢
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-10
  • 1970-01-01
相关资源
最近更新 更多