0

I encountered following error, during UI Testing.

org.openqa.selenium.WebDriverException: Element not found or not visible for xpath: (//div[@class='popupContent'])[last()]/div/div/div/div/div[2]/div/table/tbody Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z' System info: host: 'x', ip: '172.25.x.x', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_60' Driver info: driver.version: unknown

Can anyone let me know what has caused this error?

Thank you

YatShan
  • 389
  • 4
  • 18
  • This question has been answered multiple times on SO. – GPT14 Aug 23 '18 at 06:26
  • Possible duplicate of [Selenium Webdriver: Element Not Visible Exception](https://stackoverflow.com/questions/28820820/selenium-webdriver-element-not-visible-exception) – GPT14 Aug 23 '18 at 06:26

2 Answers2

1

Essentially what the stacktrace tells you:

Element not found or not visible for xpath

Most likely, you have provided an valid (syntax-wise), but incorrect xpath.

A good way of debugging (on Chrome, find your equivalent if using another browser):

  1. Navigate to the page in question
  2. Hit F12 to bring up the Developer Tools
  3. Hit CTRL+F and paste the xpath
  4. It should highlight the element in yellow

If nothing shows up and you get 0 hits with that xpath, it means that the xpath is incorrect.

Timothy T.
  • 731
  • 1
  • 9
  • 18
1

This error message...

org.openqa.selenium.WebDriverException: Element not found or not visible for xpath: (//div[@class='popupContent'])[last()]/div/div/div/div/div[2]/div/table/tbody

...implies that the WebDriver instance was unable to find any element as per the Locator Strategy you have used.


Reason

The reason for the error Element not found or not visible can be either of the following :

  • The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
  • The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
  • The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
  • The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
  • The WebElement you are trying to locate is within an <iframe> tag.
  • The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.

Solution

The solution to address NoSuchElementException can be either of the following :

  • Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.

    Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?

  • Use executeScript() method to scroll the element in to view as follows :

    WebElement elem = driver.findElement(By.xpath("element_xpath"));
    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
    

    Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium

  • Incase element is having the attribute style="display: none;", remove the attribute through executeScript() method as follows :

    WebElement element = driver.findElement(By.xpath("element_xpath"));
    ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
    element.sendKeys("text_to_send");
    
  • To check if the element is within an <iframe> traverse up the HTML to locate the respective <iframe> tag and switchTo() the desired iframe through either of the following methods :

    driver.switchTo().frame("frame_name");
    driver.switchTo().frame("frame_id");
    driver.switchTo().frame(1); // 1 represents frame index
    

    Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.

  • If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :

    • To wait for presenceOfElementLocated :

      new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
      
    • To wait for visibilityOfElementLocated :

      new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
      
    • To wait for elementToBeClickable :

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
      

JDK version issue

Apart from the above mentioned reasons/solutions, one of your major issue is the incompatibility between the version of the binaries you are using as follows:

  • Your JDK version is 1.8.0_60 which is pretty old and ancient.

So there is a clear mismatch between the JDK v8u60 and the Selenium Client v3.13.0 which you are using.

Solution

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    Thank you for the explanation. I need a small advise. If we are to test using selenium in bamboo CI nightly build, we get 10% flaky scenarios (i.e failures). Mostly are due to above mentioned error. Is there any best solution to overcome these type of failures? Does this indicate the way, we have developed application is not good? Thank you – YatShan Aug 23 '18 at 09:57
  • 1
    @Yatshan It will be tough to predict anything without a glimpse at the _HTML_ rendered by the _Web Browser_ of your choice. Factually, there is still no guidelines about the _best practices_. So until and unless you feel there is a major requirement to revamp your _Test Architecture_ you can patch up the existing _framework_ with the required enhancements. – DebanjanB Aug 23 '18 at 10:05