I have written few test cases in Selenium WebDriver using Java and execute them on grid (hub and multiple nodes). I have noticed that a few test cases fail due to NoSu
Sometimes it is possible to wait for the download of the desired item.
driver.get("https://zzzzzzzzz.market/items/mirage_prime_set")
WebDriverWait(driver, 20)
.until(
EC.visibility_of_element_located(
(By.XPATH, ('//div[@class="orders-row__element order__price sell_color"]')
)))
Sometimes you need to do something so that the UI framework loads the data. For example scroll page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
And then get the necessary data
responsetext=driver.page_source
from lxml import html
parsed_body = html.fromstring(responsetext)
obj1 = parsed_body.xpath('.//div[@class="orders-row__element order__price sell_color"]/span[1]')
print(len(obj1))
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
elementToBeClickable waits for Enable and Visible of an Element
you can also use FluentWait
,
Each FluentWait
instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions
when searching for an element on the page.
// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
Click here for more info
We can apply below codes to remove this exception condition
By applying WebDriverWait, webdriver object wait for a specific time (in second) of an element for its visibility.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(link));
We can handle NoSuchElementException through try-catch block inside Generic method
public boolean isElementPresent(By by) {
boolean isPresent = true;
try {
driver.findElement(by);
} catch (NoSuchElementException e) {
isPresent = false;
}
return isPresent
}
http://selenium-code.blogspot.in/2017/08/selenium-exception-nosuchelementexcepti.html
NoSuchElementException occurs, when the locators (i.e. id / xpath/ css selectors) is unable to find the web element on the web page.
The reasons for this could be :
Incorrect Locator
Web element not available on web page
In order to avoid this exception, we can use Fluent Wait. This wait allows us to define max timeout, polling frequency and define which exception to ignore.
Please find the sample usage of Fluent wait below :
.withTimeout(50, TimeUnit.SECONDS)
.pollingevery(3, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
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>));