显式等待不起作用,Thread.sleep工作

时间:2017-11-08 06:42:23

标签: java selenium

尝试使用显式等待,但它无效。如果添加了Thread.sleep,它可以正常工作。 Selenium WebDriver 3和Firefox 55.下面是代码。 代码

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();

还尝试了Actions类移动到元素然后单击但同样的问题。检查显示的元素,显示但仍然没有发生单击,也没有显示错误。请帮我解决这个问题。无法继续使用Thread.sleep,因为这样做不正确。

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 个答案:

答案 0 :(得分:1)

如果等待不起作用,您可以使用ID var email = resp.result.emailAddresses[0].value; 作为替代

JavascriptExecutor

答案 1 :(得分:0)

而不是:

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();

试试这段代码:

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();

答案 2 :(得分:0)

当您在等待元素时,首先应该从不声明WebElement,然后等待它。当您声明WebElement时,它应该可以在浏览器上随时用于WebDriver。

最初,你需要在wait语句中等待元素(在wait中声明或初始化元素),如下所示

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

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

然后在此处声明您的元素以进行进一步操作。

答案 3 :(得分:-1)

尝试使用隐式等待,如下所示。

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

这对我有用。