I'm getting this error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element
Why am I getting this WebDriver exception and how can I fix it?
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It only takes a minute to sign up.
Sign up to join this communityI'm getting this error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element
Why am I getting this WebDriver exception and how can I fix it?
You will get this exception when WebDriver Is not able to locate element on the page of software web application using whatever locator you have used In your test. To resolved this Issue, I will check bellow given things.
First of all I will check that I have placed Implicit wait code In my test or not. If you have not placed Implicit timeout In your test and any element Is taking some time to appear on page then you can get this exception. So I will add bellow given line at beginning of my test case code to wait for 15 seconds for element to be present on page. In 70% cases, this step will resolved Issue.
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
If Implicit wait Is already added and element locator Is fine then you need to verify that how much time It(element) Is taking to appear on page. If It Is taking more than 15 seconds then you have to put explicit wait condition with 20 or more seconds wait period as bellow. In 5 to 10% cases, This step will resolve your Issue.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
The reason behind the exception you have mentioned is because with your test script you are trying to locate an element that doesn't exist in the webpage you are trying to test.
how will you resolve It?
Well, along with the text you have provided, the console would also have given the line number in the test script where this exception occurred. I would go to that line in the test script and check what I've done wrong.
If answer to all these question is No and the element is indeed available in the webpage, I would apply implicit or explicit wait in the script to allow Selenium to wait for the element to load and then read it.
You can find the tutorials for implicit and explicit waits on the official Selenium website.