NoSuchElementException

org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.

NoSuchElementException can be thrown in 2 cases as follows :

  • When using WebDriver.findElement(By by) :

    //example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
    
  • When using WebElement.findElement(By by) :

    //example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
    

As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :

Constant Field      Type                                        Value
SESSION_ID          public static final java.lang.String        "Session ID"
e.g. (Session info: chrome=63.0.3239.108)

DRIVER_INFO         public static final java.lang.String        "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)

BASE_SUPPORT_URL    protected static final java.lang.String     "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)

Reason

The reason for NoSuchElementException 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']")));
      

Reference

You can find Selenium's python client based relevant discussion in:

  • Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Answer from undetected Selenium on Stack Overflow
Top answer
1 of 4
45

NoSuchElementException

org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.

NoSuchElementException can be thrown in 2 cases as follows :

  • When using WebDriver.findElement(By by) :

    //example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
    
  • When using WebElement.findElement(By by) :

    //example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
    

As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :

Constant Field      Type                                        Value
SESSION_ID          public static final java.lang.String        "Session ID"
e.g. (Session info: chrome=63.0.3239.108)

DRIVER_INFO         public static final java.lang.String        "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)

BASE_SUPPORT_URL    protected static final java.lang.String     "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)

Reason

The reason for NoSuchElementException 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']")));
      

Reference

You can find Selenium's python client based relevant discussion in:

  • Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
2 of 4
3

Your code is correct, I suspect the issue caused the page not complete load when you find the element.

Try add a long sleep before find element, if adding sleep worked, change sleep to wait.

Here is the code, It means waiting 10s if the element isn’t present:

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "originTextField"))
)
🌐
Selenium
selenium.dev › documentation › webdriver › troubleshooting › errors
Understanding Common Errors | Selenium
Make sure you are on the page you expect to be on, and that previous actions in your code completed correctly ... An element goes stale when it was previously located, but can not be currently accessed.
🌐
TutorialsPoint
tutorialspoint.com › unable-to-locate-an-element-using-xpath-error-in-selenium-java
Unable to locate an element using xpath error in selenium-java
This type of exception is thrown when there is no element on the page which matches with the locator value. If error is encountered, we can fix it by the following ways − · Check if there is any syntax error in our xpath expression. Add additional expected wait conditions for the element.
🌐
Reflect
reflect.run › articles › everything-you-need-to-know-about-nosuchelementexception-in-selenium
Everything you need to know about NoSuchElementException in Selenium | Reflect
NoSuchElementException is one of ... element cannot be found. A NoSuchElementException occurs when the Selenium locator strategy defined is unable to find the desired HTML element in the web page....
🌐
Quora
quora.com › How-do-you-handle-being-unable-to-locate-an-element-in-Selenium
How to handle being unable to locate an element in Selenium - Quora
Answer: Hello, 1. Check in the event that there is any language structure mistake in our xpath expression. 2. Add extra expected wait conditions for the element. 3. Utilize an option xpath expression.
🌐
Quora
quora.com › How-can-I-solve-this-error-on-Java-Eclipse-Selenium-code-no-such-element-Unable-to-locate-element-even-it-was-working-fine-before
How to solve this error on Java Eclipse Selenium code 'no such element: Unable to locate element' even it was working fine before - Quora
"No such element: Unable to locate element" in Selenium means WebDriver couldn't find the DOM node with the locator you supplied. If the code worked before and now fails, likely causes are timing, page structure changes, frames/iframes, dynamic ...
Find elsewhere
🌐
GitHub
github.com › SeleniumHQ › selenium › issues › 2419
NoSuchElementException: Unable to locate element in Selenium webdriver · Issue #2419 · SeleniumHQ/selenium
Unable to locate element error is displayed org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Register/Create an Account"} Command duration or timeout: 0 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00' Session ID: 8757a2e4-a195-4ce0-b043-0f6c3decacc8 Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnab
🌐
Testmuai
testmuai.com › testmu ai › blog › how to fix nosuchelementexception in selenium | testmu ai
How to Fix NoSuchElementException in Selenium | TestMu AI (Formerly LambdaTest)
NoSuchElementException is one of the most common exceptions in Selenium, which occurs when an element is not found on the page. This blog post discusses NoSuchElementException, why it occurs, and how to fix NoSuchElementException in Selenium with Java.
Published   February 17, 2026
🌐
Quora
quora.com › In-Selenium-WebDriver-after-the-launching-browser-I-am-getting-an-error-as-unable-to-locate-element-even-if-the-locator-is-correct-What-can-be-done-to-be-successful
In Selenium WebDriver, after the launching browser, I am getting an error as unable to locate element even if the locator is correct. What can be done to be successful? - Quora
Answer (1 of 10): Check the following things:- 1. Check whether you are trying to locate web element in the correct frame. If not switch the frame and then try to locate it. 2. Make sure you are using the correct by class methods for your locator.
🌐
Google Groups
groups.google.com › g › selenium-users › c › NBtdYTvh8OY
Unable to locate element Error using Webdriver
NoSuchElement found error message will come when the selenium is trying to find the Element on look up. Example: Go to Salesforce.com, generate a testCase for creating a new Account and save it. Now click on name field a look will appear........... Selenium cannot locate such Elements.
🌐
testRigor
testrigor.com › home › no such element exception in selenium: how do you handle it?
No Such Element Exception in Selenium: How Do You Handle It? - testRigor AI-Based Automated Testing Tool
October 16, 2024 - NoSuchElementException is thrown by findElement() method in Selenium WebDriver when the desired element cannot be located using the specified locator (such as an ID, name, class, CSS selector, or XPath).
🌐
GitHub
github.com › seleniumhq › selenium-google-code-issue-archive › issues › 5047
Unable to locate element: {"method":"xpath","selector":"//*[@id='usernameTextEdit']"} · Issue #5047 · SeleniumHQ/selenium-google-code-issue-archive
March 3, 2016 - ----------------------------------------------------------- public class Login { public static void main(String[] args) { WebDriver d1= new FirefoxDriver(); d1.navigate().to("http://inlv50795915s:8080/InfoViewApp"); d1.findElement(By.xpath("//*[@id='usernameTextEdit']")).sendKeys("administrator"); } } --------------------------------------------------------------- Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='usernameTextEdit']"} Command duration or timeout: 188 milliseconds For documentation on this erro
🌐
DEV Community
dev.to › liviufromendtest › how-to-fix-the-element-not-found-error-in-selenium-28gj
How to fix the "Element not found" error in Selenium - DEV Community
July 13, 2020 - Not properly configuring the Explicit Wait is why you'll hear some people say "Oh, I tried Selenium, it doesn't work, my tests are too flaky". If you ever find that claim in an article, you should unfollow the author. In Endtest, you can configure the Element Load Timeout for each test suite, this means that you don't have to add a wait before each step. You can find more details about that in this chapter. A dynamic locator is an attribute of an element that does not keep the same value over time.
🌐
Google Groups
groups.google.com › g › selenium-users › c › OMgZGydnn0I
Xpath Correct but still get no such element: Unable to locate element:
FileInputStream urlFile = new FileInputStream("C:\\Users\\Ashish\\Documents\\Selenium\\SeleniumPracticeSite\\src\\URL.properties"); ... myDynamicElement.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='userName']"))); ... <tr> <td align="right"><font face="Arial, Helvetica, sans-serif" size="2">User Name: </font></td> <td width="112"> <input type="text" name="userName" size="10"> </td> ... http://stackoverflow.com/questions/39561188/xpath-is-correct-still-get-no-such-element-unable-to-locate-element?noredirect=1#comment66434922_39561188