检查按钮是否可用?如果不等5秒钟再检查一次? [英] check if a button is available? if not wait 5 seconds and check again?

查看:81
本文介绍了检查按钮是否可用?如果不等5秒钟再检查一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试查看当前是否可以单击按钮.如果没有,我想再试一次.因此,我需要某种goto函数以返回到我的代码的较早代码行.虽然我怀疑我写得很差,但可以做得容易得多.

Basically I'm trying to see if a button is able to be clicked at the moment. If not I would like to try again. So I need some kind of a goto function to return to an earlier line of my code. Although I suspect I written this extremely poorly and it could have been done much easier.

try {
    driver.findElement(By.xpath("//button[@id='btn_ok']")).click();
}catch (Exception e) {
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}   

对于上下文,这是有问题的按钮元凶.

for context, here is the button culprit in question.

<button type="submit" value="ok" name="s1" id="btn_ok" class="green">

推荐答案

您可以使用流利的方法来等待.这将检查按钮是否每5秒钟单击一次,持续30秒钟.您可以根据需要调整时间.尝试使用此代码,并给出反馈是否有效.

You can use fluent wait for this. This will check for the button to be clickable at every 5 seconds for 30 seconds. You can adjust the time according to your need. Try this code and give feedback whether it worked or not.

 Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)                           
                .withTimeout(30, TimeUnit.SECONDS)          
                .pollingEvery(5, TimeUnit.SECONDS)          
                .ignoring(NoSuchElementException.class);
        WebElement clickseleniumlink = wait.until(new Function<WebDriver, WebElement>(){

            public WebElement apply(WebDriver driver ) {
                return driver.findElement(By.xpath("//button[@id='btn_ok']"));
            }
        });
        clickseleniumlink.click();

这篇关于检查按钮是否可用?如果不等5秒钟再检查一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆