I'm working on a Java Selenium-WebDriver. I added
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
and
WebElement textbox = driver.findElement(By.id("textbox"));
because my Applications takes few seconds to load the User Interface. So I set 2 seconds implicitwait. but I got unable to locate element textbox
Then I add Thread.sleep(2000);
Now it works fine. Which one is a better way?
Answer : wait for few seconds before element visibility using Selenium WebDriver go through below methods.
implicitlyWait() : WebDriver instance wait until full page load. You muse use 30 to 60 seconds to wait full page load.
ExplicitlyWait WebDriverWait() : WebDriver instance wait until full page load.
Note : Please use ExplicitlyWait WebDriverWait() to handle any particular WebElement.
is the worse: being a static wait, it will make test script slower.
this is a dynamic wait
Finally, what I suggest is
with some predefined conditions:
If using webdriverJs (node.js),
The code above makes browser wait for 5 seconds after clicking the button.
click appears to be blocking? - here's another way to wait if you're using WebDriverJS:
The code above waits after the button is clicked for the next page to load and then grabs the source of the next page.
This thread is a bit older, but thought I'd post what I currently do (work in progress).
Though I'm still hitting situations where the system is under heavy load and when I click a submit button (e.g., login.jsp), all three conditions (see below) return
true
but the next page (e.g., home.jsp) hasn't started loading yet.This is a generic wait method that takes a list of ExpectedConditions.
I have defined various reusable ExpectedConditions (three are below). In this example, the three expected conditions include document.readyState = 'complete', no "wait_dialog" present, and no 'spinners' (elements indicating async data is being requested).
Only the first one can be generically applied to all web pages.
Depending on the page, I may use one or all of them:
There are also predefined ExpectedConditions in the following class: org.openqa.selenium.support.ui.ExpectedConditions
I prefer the following code to wait for 2 seconds.