Understanding No Such Element Exception in Selenium
Related Product On This Page What is NoSuchElementException in SeleniumApril 23, 2026 · 10 min read · Tool Comparison
Most of the websites in today ’ s world are dynamical, which means they load the web pages and web elements lazily. A dynamic website generates and loads web pages in real-time as per the blind size, device type and browser. Dynamic websites often update their DOM message in real-time using technologies such as JavaScript or AJAX. Due to this dynamic behavior, web elements on the page might not be available immediately when you try to interact with them which often leads to aNoSuchElementExeption. Most of the websites in today ’ s world are dynamic which means it loads the web page and web elements lazily. A dynamical website generates and loads the web pages in real time as per the screen size, device type and browser. NoSuchElementExceptionis shed by the WebDriver when the desired web element can not be situate using the specified locator, such as ID, name, CSS Selector, or XPath. This exception indicates that at the time of executing, the web element you are trying to interact with is not present on the webpage. There are two types of exceptions, NoSuchElementExceptionis an unchecked elision which is a subclass ofNotFoundException NoSuchElementExceptionin Selenium occurs in the following cause: Some web elements are really dynamic in nature. In such cases the specified locater can not be access by Selenium WebDriver as its place continue on alter have NoSuchElementException. For example, while automate, the locater value was“ //a [@ class= ’ abc_123 ’] ”, however while executing the test the class value got modify to“abc_456”. Such changes in the DOM structure result to No Such Element Exception in Selenium. At times, the component ’ s visibility depends on certain weather. If those conditions are not met at the time of execution, Selenium will cast NoSuchElementException. For example, suppose in an automation playscript, the element to be snap is visible only after checking a particular radio button. In that case, if for any reason WebDriver fails to tick the radiocommunication button, the desired ingredient is not visible and hence an elision will be cast. Some elements might take some time to load due to various constituent such as mesh speed, active rendering or JavaScript execution. Selenium execution is really tight. Hence, in such cases, for dynamic web covering, if the WebDriver activity on the factor lead property before the rendering of the element, NoSuchElementException is drop. Selenium provides 8 locater strategies, among which some of the most wide utilise are ID, name, and. If the locator strategy used to find the element is incorrect, Selenium will shed the exception. Read More: Sample code to click offers tab in BStackDemo app: Here, the “ volunteer ” webelement is throw incorrect locator value, as the correct locator should be “ a # offers strong ”. Hence, Selenium will shed NoSuchElementException. Here are different ways to handle NoSuchElementException in Selenium: WebDriverWait allows you to wait for an element to satisfy a specific condition defined through the. For example, you can use WebDriverWait to wait for some seconds until the coveted element is present in the DOM. Below example demonstrate pilot toBStackDemocoating, clicking on the pass tab and verifying that it bring to Sign In page. In the above program, “ offers ” and “ username ” web elements are created and they have been associated with WebDriver expect which state them to wait for 10 seconds so that the interacted web element gets rendered completely to have any commands later by Selenium WebDriver. Surround the code cube which you feel may throw a NoSuchElementException with a try-catch block. This facilitate to catch the elision if it come and let the codification continue to work without abruptly stopping the execution. In the try block, add the code which may cast an exception and in the match block add the required activeness to handle the exclusion graciously. Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. In the above code, a web factor “ offers ” is created and in the try block it is clicked. If any exception occurs the executing control goes to the match cube and click the offers link. findElement () method of WebDriver returns the first web component matching the locater whereas findElements () returns a list of web elements matching the locator. If the web element is create utilize findElement () method and the web element is not present in the DOM, Selenium will surely cast a NoSuchElementException. To obviate this, you can use findElements () method which returns a list of web elements and by ascertain the size of the list, the visibleness of the web element can be find. Read More: Locators such as ID and gens should be preferably utilize while create locators. And if there is no unique ID or name, XPath should be used. It is always recommended to use proportional XPath locators instead of absolute as it is less probable to be changed in case of DOM structure change hence reducing the chances of elision. By researching this way, the almost suitable locator should be used to avert NoSuchElementException. Sample codification to snap on offering join onBStackDemo application: Some web elements are inside frame/ iframe and it is only discovered when you encounter an exception. Always control if the component is inside any frame and in such case switch to the frame/ iframe and then do the action on the desire web component. After the action is consummate, switch the driver back to the parent window by usingdriver.switchTo () .defaultContent (). Read More: Just to certify the switching of driver, presume that inBStack Democovering, “ offers ” link is inside a frame. Below codification shows how to switch to the frame, perform desired action on the web element and switch back to the nonremittal content. It is pivotal to adhere to some best practices while writing mechanisation scripts to avoid encountering the NoSuchElementException. By following these best practices, you ensure that the test scripts are robust, reliable and capable of dealing with dynamic message. At times, some web elements are not visible and intractable due to dynamic page load, sheathing or average overlap issues. Ensure at all times that the element is just not present in DOM but is also visible and interactable. Thread.sleepmethod is always handy whenever you want to pause the examination executing temporarily. However, it is unreliable for dynamical websites where the loading of web elements is incertain. This could lead to weather where your script proceeds before elements are completely loaded, leading to NoSuchElementException. Using absolute XPaths or very complex CSS Selectors can often cause subject chance the right web constituent on a web page due to dynamic DOM structure. Always use robust and stable locators, which are less prone to break if the DOM construction changes, such asid, name, and class. Page Object Model is a design pattern in Selenium that creates an object secretary to store all web elements. It aid create more maintainable and clear codification by abstracting element locators and actions into freestanding classes which aids in reducing code duplication and improves test case alimony. So, whenever any locator value is changed in DOM, you can just update the related POM file where the locater resides instead of modify each test case employ that locater. To demonstrate the NoSuchElementException, consider an automation script for a website with an input box inside an iframe. If the book attempts to enrol schoolbook into the input box without first switching to the iframe, a NoSuchElementException will occur. Selenium script: Run the above program and see the output. In the above codification, input box is defined and sendKeys method is used to enter text. However, this will drop a NoSuchElementException as the comment box is present inside an iframe and the driver has not shift to the iframe before interacting with the stimulus box. To fix this exception, you need to exchange to the iframe and so interact with the element. So, the updated codification should be as below. Handling dynamic web element in Selenium is crucial for robust tryout mechanisation. By using expressed waits, correct locator scheme, frame handling, and try-catch mechanisms, one can effectively deal with dynamical elements in the automation hand. It may also occur that NoSuchElementException may arise in some specific device, which demands to prove the applications on existent devices and browser. In today ’ s ever growing mobile marketplace it is not feasible to procure and test on all the latest devices and browsers. To cope with this issue, it is best to invest in any platform which aids in testing the application on a wide range of devices and browsers. is one of the cloud-based platform that provides a comprehensive infrastructure for cross-device and with zero hassle in setting up the gimmick and environment. Testing coating on and browsers is obligatory to ensure a positive user experience, dependability, performance and compatibility of the application across a divers range of device, browsers and platforms. offers a comprehensive platform for running Selenium examination, providing respective key reward: To plow NoSuchElementException, there are several approaches such as creating robust locator, using try-catch blocks, and applying WebDriverWait, etc. Additionally, this exception may occur on specific devices or browsers. To address this, it ’ s essential to test Selenium scripts across real devices and browser. And when it get to prove on a wide compass of devices, browsers, and OS combinations, is the ideal solvent. It provides an broad program for running Selenium tests across different environs, see better coverage and reliability. On This Page # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Related Product
Understanding No Such Element Exception in Selenium
What is NoSuchElementException in Selenium
When does No Such Element Exception in Selenium occur?
1. Changes in DOM structure
2. Element not present
3. Element not yet lade
4. Incorrect Locator Strategy
public nullity verifyOffersClick () {driver.get (`` https: //bstackdemo.com/ ''); WebElement offers= driver.findElement (By.cssSelector (`` a # offer potent '')); offers.click ();}How to handle NoSuchElementException in Selenium
1. Using WebDriverWait
public family NoSuchElementException {WebDriver driver=new ChromeDriver (); @ Test public void verifyOffersClick () {driver.get (`` https: //bstackdemo.com/ ''); WebDriverWait wait =new WebDriverWait (driver, Duration.ofSeconds (10)); By offer= By.cssSelector (`` a # pass strong ''); WebElement offers=wait.until (ExpectedConditions.elementToBeClickable (offer)); offers.click (); By uname= By.xpath (`` //div [text () ='Select Username '] ''); WebElement username=wait.until (ExpectedConditions.presenceOfElementLocated (uname)); Assert.assertTrue (username.isDisplayed ());} @ AfterTest public void tearDown () {driver.quit ();}}2. Using Try-Catch block
@ Test public nothingness verifyOffersClick () {driver.get (`` https: //bstackdemo.com/ ''); WebElement offers=driver.findElement (By.cssSelector (`` a # offers strong '')); try {offers.click ();} catch (Exception e) {e.printStackTrace (); offers.click ();}}3. Use findElements () instead of findElement ()
@ Test populace nothingness verifyOffersClick () {List & lt; WebElement & gt; offers= driver.findElements (By.cssSelector (`` a # go strong '')); if (offers.size () & gt; 0) {offers.get (0) .click ();}}4. Use more reliable selectors
public void verifyOffersClick () {driver.get (`` https: //bstackdemo.com/ ''); WebElement offers= driver.findElement (By.cssSelector (`` a # fling strong '')); offers.click (); driver.manage () .timeouts () .implicitlyWait (Duration.ofSeconds (2)); WebElement username= driver.findElement (By.xpath (`` //div [text () ='Select Username '] '')); Assert.assertTrue (username.isDisplayed ());}5. Switch to frame/ iFrame
@ Test public void verifyOffersClick () {driver.switchTo () .frame (`` 1 ''); WebElement offers= driver.findElement (By.cssSelector (`` a # offers strong '')); offers.click (); driver.switchTo () .defaultContent ();}Best Practices to Avoid NoSuchElementException
1. Check the Element ’ s Visibility and Interactivity
2. Avoid Thread.sleep Method
3. Use Robust Locators
4. Use POM (Page Object Model)
Real-Time Example of how to fix NoSuchException in Selenium
importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importorg.testng.annotations.Test; public family NoSuchElementExp {WebDriver driver = new ChromeDriver (); @ Test (priority = 1) public void inputTest () {driver.get (`` https: //demo.automationtesting.in/Frames.html ''); WebElement iframe=driver.findElement (By.xpath(`` //iframe [@ name='SingleFrame '] '')); driver.switchTo () .frame (iframe); WebElement input= driver.findElement (By.xpath(`` (//input [@ type='text ']) [1] '')); input.sendKeys (`` Selenium ''); driver.close ();}}public class NoSuchElementExp {WebDriver driver = new ChromeDriver (); @ Test (antecedency = 1) public vacuum inputTest () {driver.get (`` https: //demo.automationtesting.in/Frames.html ''); WebElement iframe=driver.findElement (By.xpath(`` //iframe [@ name='SingleFrame '] '')); driver.switchTo () .frame (iframe); WebElement input= driver.findElement (By.xpath(`` (//input [@ type='text ']) [1] '')); input.sendKeys (`` Selenium ''); driver.close ();}}Why run Selenium Tests on Existent Devices and Browsers?
Why choose BrowserStack to run Selenium Tests?
Conclusion
Related Guides
Automate This With SUSA
Test Your App Autonomously