Explicit wait not working, Thread.sleep works

Viewed 2227

Trying to use explicit wait but it is not working. If Thread.sleep is added it works perfectly fine. Selenium WebDriver 3 and Firefox 55. Below is the code. Code

WebDriverWait wait=new WebDriverWait(driver, 30);
WebElement w1 = driver.findElement(By.xpath("//*[@id='formdesigner']"));
wait.until(ExpectedConditions.elementToBeClickable(w1));
driver.findElement((By.xpath("//*[@id='formdesigner']"))).click();

Have also tried Actions class move to element and then click but same problem. Checked for element displayed, it is displayed but still no click happening and no error displayed. Please help me to get the solution for this. Can't keep on using Thread.sleep as it is not correct to do so.

HTML code:

HTML code

<div class="col-sm-6 full-height">
    <div id="formdesigner" class="row full-height">
        <div class="col-sm-12 tile-name">FORM DESIGNER</div>
        <div class="col-sm-12 tile-image">
            <div class="link-img"/>
        </div>
    </div>
</div>
4 Answers

Instead of :

WebDriverWait wait=new WebDriverWait(driver, 30);
WebElement w1 = driver.findElement(By.xpath("//*[@id='formdesigner']"));
wait.until(ExpectedConditions.elementToBeClickable(w1));
driver.findElement((By.xpath("//*[@id='formdesigner']"))).click();

Try this code block :

WebDriverWait wait2 = new WebDriverWait(driver, 10);
WebElement w2 = wait2.until(ExpectedConditions.elementToBeClickable((By.xpath("//div[@id='formdesigner']/div[@class='col-sm-12 tile-name']"))));
w2.click();

When you are waiting for element, at first you should never declare WebElement and then wait for it. When you declare a WebElement it should be readily available for WebDriver on browser.

Initially, you need to wait for element(declare or initialise element inside wait) in wait statement like below

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("your xpath or id")));

or

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("your xpath")));

and then declare your element here for further operations.

You can use ID with JavascriptExecutor as alternative if wait is not working

WebElement element = driver.findElement(By.id("formdesigner"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Try using implicit wait as shown below.

    WebElement w1 = driver.findElement(By.xpath("//*[@id='formdesigner']"));
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.findElement((By.xpath("//*[@id='formdesigner']"))).click();

This worked for me.

Related