You can never be sure that element will be found, actually this is purpose of functional tests - to tell you if anything changed on your page. But one thing which definitely helps is to add waits for the elements which are often causing NoSuchElementException like
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
Answer from Petr Mensik on Stack OverflowYou can never be sure that element will be found, actually this is purpose of functional tests - to tell you if anything changed on your page. But one thing which definitely helps is to add waits for the elements which are often causing NoSuchElementException like
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
I completely agree to Petr Mensik above. The matter you can never say whether element is present. You should clearly understand why when it happens. From my experience I should say that it happens due to the following reasons:
- 1) The page is still being rendered and you've already finished your element search and obtain no element exception.
- 2) The second reason is AJAX has not returned yet and you've already
obtain
NoSuchElementException - 3) The third is most obvious: The element is really not on the page whenever.
so the most robust IMHO way to handle all these three conditions using one function call is to use fluentWait as Amith003 suggested.
so the code be the following:
let ur element has the locator:
String elLocXpath= "..blablabla";
WebElement myButton= fluentWait(By.xpath(elLocXpath));
myButton.click();
public WebElement fluentWait(final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(org.openqa.selenium.NoSuchElementException.class);
WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
}
);
return foo;
};
Also if your purpose is robust code wrap fluentWait() with a try{} catch{} block.
Also don't forget about
public boolean isElementPresent(By selector)
{
return driver.findElements(selector).size()>0;
}
that is also useful.
So to conclude all the mentioned if you want to avoid NoElement exception just handle it properly as nobody can ensure in the element presence on the page.
Hope now it is more clear to you. Regards
selenium.common.exceptions.NoSuchElementException Error : Forums : PythonAnywhere
NoSuchElementException: no such element: Unable to locate element
Selenium Python - Handling No such element exception - Stack Overflow
Selenium Python NoSuchElementException
Videos
Hi,
I'm very new to selenium and writing my first script which is a Google search. Th script has trouble clicking on the search button after typing in what to search for. I get a 'NoSuchElementException: no such element: Unable to locate element'. I also changed the script so it presses enter rather than clicking on the button and got the same error. Can you help? I have placed links to screenshots of my code and the error.
https://imgur.com/a/TeNy66B
Thanks
Are you not importing the exception?
Copyfrom selenium.common.exceptions import NoSuchElementException
try:
elem = driver.find_element_by_xpath(".//*[@id='SORM_TB_ACTION0']")
elem.click()
except NoSuchElementException: #spelling error making this code not work as expected
pass
You can see if the element exists and then click it if it does. No need for exceptions. Note the plural "s" in .find_elements_*.
Copyelem = driver.find_elements_by_xpath(".//*[@id='SORM_TB_ACTION0']")
if len(elem) > 0
elem[0].click()