3

Using geckodriver 0.23.0, firefox 64.0.2, selenium 3.12, java 8 I'm not able to find the element by partial link text. A frame is not used. Link text is "Accounts (1)". There is only one other instance of the same text on the page "View All Accounts"

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>

The code I'm using to find the element: "Accounts (n)" where n = 1, 2, 3 ...

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

I tried with "Accounts " and with "Accounts (" and they both return the same 404 not found - no such element error

Console log:

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"}}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ivana
  • 85
  • 6
  • If finding "Accounts" by partial link text is not working, there's something wrong. Have you tried adding a wait? Are you sure there isn't an IFRAME? Are you sure of the capitalization? – JeffC Jan 16 '19 at 23:03
  • Try running `$x("//a[.='Accounts (1)']")` in the dev console. Does it find anything? – JeffC Jan 16 '19 at 23:06

1 Answers1

0

As you mentioned you are trying to find the element with text as Accounts (n) where n = 1, 2, 3 ... and a couple of more elements with linkText as Add New Account and View All Accounts exists, instead of using partialLinkText it would be better to use XPath and you can use the following solution:

  • 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();
    

Update

As per the discussion Official locator strategies for the webdriver Partial link text selector is preferred than XPath selector. However as per this usecase due to presence of similar link texts it would be easier to construct a XPath.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Good answer... yet is it better to use XPath then partialLinkText? – Moshe Slavin Jan 16 '19 at 10:22
  • 1
    @MosheSlavin Added an update to the answer. Let me know if that answers your counter question. – undetected Selenium Jan 16 '19 at 11:10
  • 1
    I have seen this discussion hence my answer and comment... I must say I learn from you a lot!!! thanks – Moshe Slavin Jan 16 '19 at 11:13
  • What confuses me regarding the partialLinkText is inability to locate a unique name: ---- Add New Account – does not have s at the end -> should not be catch by search “Accounts “ or “Accounts (“ ---- View New Accounts – does not have space at the end -> should not be catch by search “Accounts “ or “Accounts (“ ---- Am I missing something here? – Ivana Jan 16 '19 at 17:51
  • @Ivana As you target text is `Accounts (n)`, targeting the partial text `Accounts` may include `Accounts (1)` and `View All Accounts` both. On the other hand I strongly would avoid `Accounts (` as the **`(`** can induce much chaos within the [Locator Strategy](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890). Hence I would recommend _xpath_ or a _cssSelector_. – undetected Selenium Jan 16 '19 at 18:00