Issue
I'm getting the what seems fairly common NoSuchElementException
when waiting on an element to load in the DOM. I've used a fluent wait and ignored it but it still seems to be happening. Tests are passing, it does eventually locate the element.
Code:
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
.pollingEvery(Duration.ofMillis(500))
.ignoring(ElementNotInteractableException.class, NoSuchElementException.class);
Stack:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[class="fsrAbandonButton"]"}
(Session info: headless chrome=118.0.5993.88)
I'm confused as to why it would still output this error when I'm explicitly telling it to ignore this?
Solution
I have little to no experience in FluentWaits, but there are several causes for Selenium not finding an element with NoSuchElementException. You might want to use WebDriverWait instead of FluentWait, since it's giving you a lot of options to choose from. Using these options the correct way helps you find the element the right way.
Let's first initialize a WebDriverWait, simple:
WebDriverWait wait = new WebDriverWait(driver, 30);
The enormous amount of ExpectedConditions are given in this documentation. But I would advise you two of the most frequently and simple ones you can use:
- visibilityOfElementLocated (by locator)
This one is my preferred way, since it's checking (1) if the element exists and (2) it's visible in the window/viewport. That way you have most guarantee you can interact with the element.
wait.until(ExpectedConditions.visibilityOfElementLocated(By locator));
- presenceOfElementLocated (by locator)
If you come across TimeoutException
or NoSuchElementException
, this would be a good choice. Since it's now scanning the entire DOM and it doesn't need to be visible in your screen. You might have issues when interacting with the element like click()
, but it's your best choice if you need to just see if the element exists.
wait.until(ExpectedConditions.presenceOfElementLocated(By locator));
Solution When having issues with NoSuchElementException, choose for Waits where you will see if the element Exists.
Answered By - kenneth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.