Not able to find element by partial link text using Java and Selenium

Ivana :

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

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109449&siteId=1
Recommended