Element not found in a runtime



  • I am encountering a very unusual thing, the one I haven't seen before. There is one element I needed to find for testing purposes, it is visible on the page and I can find its unique relative XPath with rclick-->inspect option, but in run time it is not recognized. Here is more info:

    1. I know for sure that the element is not in any of the frames because I checked and all frames have different XPath.

    2. I tried to get all elements of the page and print their text (the element I need does have text) with //* option and the text I needed wasn't printed

    3. I tried full XPath and very strangely beside the element I need, there is another element with the same full XPath. For sure relative XPath can be duplicate, but I thought it is not possible with full XPath. How can it happen?

    4. Also, when I used that full XPath to put all elements in a list, the size of the list was 1. In other words, it found the other element, not the one I needed. How can all that happen?

    The XPaths are:

    • relative - //*[@id='NoChainFound']
    • absolute - /html/body/div[5]/div[2]

    The HTML I am looking at is:

    <div id="NoChainFound" ng-show="!inGantt &amp;&amp; isChainEmpty() &amp;&amp; serviceId">This service appointment has no scheduling dependencies. 
        <a class="learn-more" href="help.salesforce.com/articleView?id=pfs_complex_work.htm" target="_blank">Learn More</a>
    </div> 
    

    My code is:

    WebElement message = driver.findElement (By.xpath("//*[@id='NoChainFound']"));
    

    And the error is:

    no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='NoChainFound']"}(Session info: chrome=79.0.3945.88)(Driver info: chromedriver=2.40.565498
    


  • This could be a timing issue(as most of the scenario you already discussed) when you're going to find it would not be present at that time on DOM, so you should try using WebDriverWait to wait until this element could be present as below:-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("messageStackError")));
    el.getText()
    

    One more thing you can try use findElements instead of findElement, findElements will return an empty list if no matching element is found instead of an exception. this will help you to find the root cause of the issue atleast.

    Boolean isPresent = driver.findElements(By.yourLocator).size() > 0
    

    Just make sure one more time before finding the element that it is not inside any frame or iframe. If it is inside then you need to switch that frame or iframe before finding element as driver.switchTo().frame("frame id or name");



Suggested Topics

  • 2
  • 2
  • 2
  • 3
  • 2
  • 2
  • 2
  • 2