selenium wait for element to be clickable
How do I wait for an element to be enabled in Selenium?
As you knows, In Selenium IDE software testing tool we can use “waitForElementPresent” or “verifyElementPresent” to wait for or verify that element is present or not on software web application page. In selenium webdriver, we can do same thing using explicit wait of elementToBeClickable(By locator). Full syntax is as bellow.
Read more tutorials on selenium WebDriver @Tutorials Part 1 and @Tutorials Part 2.
Check if element is clickable Selenium Java
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
Selenium wait for element to be clickable java Example
package junitreportpackage; import java.util.concurrent.TimeUnit; import java.io.FileInputStream; import java.io.IOException; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; public class Mytest1 { WebDriver driver = null; @Before public void beforetest() { System.setProperty("webdriver.gecko.driver", "D:\Selenium Files\geckodriver.exe"); driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html"); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); } @After public void aftertest() { driver.quit(); }
@Test public void test () { driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name"); //Wait for element to be clickable Selenium java WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton"))); driver.findElement(By.cssSelector("#submitButton")).click(); }
public void HighlightMyElement(WebElement element) { for (int i = 0; i < 10; i++) { JavascriptExecutor javascript = (JavascriptExecutor) driver; javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: orange; border: 4px solid orange;"); javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: pink; border: 4px solid pink;"); javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: yellow; border: 4px solid yellow;"); javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); } } }
Run above given example of selenium wait until element is clickable in your eclipse with junit. It will show you how to wait for element to be clickable in selenium java. Click here to view different posts on how to use junit with eclipse for your webdriver test.
11 thoughts on “How to wait for element to be clickable in selenium webdriver using explicite wait”
Its possible to highlight the element and wait until capture the screenshots ??
Hi, Can u please explain what is use of for loop here ?
can anyone explains why we have used highlightmyelement func()
I have some questions:
Since you have the code below which will wait for 15seconds
->driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Will this add up on the code below?
->WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
If submit button is not yet clickable, does it mean it will wait for 15seconds?
I assume that because as I understand implicitWait is one time setup.
Is it really recommended to use implicit rather than explicit?
Implicit will make your testing time longer…
implicitlyWait will wait till given time(15 seconds) if targeted element not found on page. It will apply to whole script. So it is recommended to use.
We use explicit wait in special conditions only like some element is taking too much time to be visible, waiting for alert or some text etc.. Hope this will clear your doubt.
HI i have some doubt for example we given 15 seconds in implicit wait, the element is clickable with in the 10 seconds. It is wait until 15 seconds even though the element is click able before 15 seconds or it it will continue to next.
Same as Explicit
can u give the answer please i faced this question in one of the interview
@tulasireddy: Im sure it will click on button if its clickable within 10 seconds
i have used same code but it's not works
HI ,implicit wait will wait upto 15 seconds,when selenium finds element at 10 seconds , it will continue the process .
Eplicit wait : will wait for 15 seconds no matter if element present or not (if the element present also in 10th seconds it should wait for 15 seconds .)
HI ,implicit wait will wait upto 15 seconds,when selenium finds element at 10 seconds , it will continue the process .
Eplicit wait : will wait for 15 seconds no matter if element present or not (if the element present also in 10th seconds it should wait for 15 seconds .)
if implicit wait finds the element withing 10 seconds it will start process and will ignore remaining 5 seconds.
And if you have applied explicit wait also then explicit wait will also try to find the element even though implicit wait already have found but , you have written the line of code for explicit wait and the line of code will execute and explicit wait will try to find and lets say explicit wait found the element in 2 seconds then code will execute…Reason why explicit wait will still try to find the element because it will check and and make sure it has found the element and then code will execute so it will also have its own sureity by checking.