检查按钮是否可用?如果不是等待5秒再检查一下?

问题描述 投票:4回答:5

基本上我正试图看看是否能够点击一个按钮。如果不是,我想再试一次。所以我需要某种goto函数来返回我的代码的早期行。虽然我怀疑我写得非常糟糕,而且本来可以做得更轻松。

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

对于上下文,这里是问题的按钮罪魁祸首。

<button type="submit" value="ok" name="s1" id="btn_ok" class="green">
java selenium selenium-webdriver wait goto
5个回答
2
投票

您可以使用流利的等待。这将检查按钮是否每5秒可点击30秒。您可以根据需要调整时间。尝试此代码并提供反馈是否有效。

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

1
投票

试试这种方式。看看它是否有帮助。

int size=driver.findElements(By.xpath("//button[@id='btn_ok']")).size();
if (size>0)
  {
    driver.findElement(By.xpath("//button[@id='btn_ok']")).click();
  }
  else
  {
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    int size1=driver.findElements(By.xpath("//button[@id='btn_ok']")).size();
    if (size1>0)
  {
    driver.findElement(By.xpath("//button[@id='btn_ok']")).click();
  }



}   

0
投票

您可以使用显式等待来等待按钮可单击。它将每隔500毫秒测试一次按钮,持续最长指定时间,直到可点击为止

WebDriverWait wait = new WebDriverWait(driver, 5); // maximum wait time is 5 here, can be set to longer time
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("btn_ok")));
button.click();

作为旁注,implicitlyWait设置驱动程序搜索元素的最大时间,它不会延迟脚本。


0
投票

我更喜欢这个,因为从字面上看,任何布尔条件都可以“等到”。

    public static void WaitUntil(this IWebDriver driver, Func<bool> Condition, float timeout = 10f)
    {

        float timer = timeout;
        while (!Condition.Invoke() && timer > 0f) {

            System.Threading.Thread.Sleep(500);
            timer -= 0.5f;

        }
        System.Threading.Thread.Sleep(500);

    }

    driver.WaitUntil(() => driver.FindElements(By.XPath("some xpath...").Length == 0);

    //Here is the particular benefit over normal Selenium waits. Being able to wait for things that have utterly nothing to do with Selenium, but are still sometimes valid things to wait for.
    driver.WaitUntil(() => "Something exists in the database");

我发现隐性等待给我带来的麻烦比它值得多。而且我发现明确的selenium等待可能会有点冗长,而且它并没有涵盖我在框架中需要的所有东西,所以我已经做了很多扩展。这是其中之一。注意,我在上面的示例中使用了FindElements,因为如果没有找到任何内容,我不希望抛出异常。这应该适合你。

注意:这是C#,但对于任何语言(尤其是Java)修改它都应该不难。如果您的语言不允许这样的扩展,只需直接在类中调用该方法即可。您需要将它放在静态类中才能起作用。在逻辑中扩展这样的现有类时要小心,因为当他们试图确定方法的定义时,可能会混淆其他类。


0
投票

快速回答

请在下面的代码中查看并告知它是否解决了您的问题。

    public synchronized boolean clickOnButtonWhenItBecomesClickable() {
        boolean buttonClicked=false;
        try {
            List<WebElement> element = driver.findElements(By.xpath("//button[@id='btn_ok']"));

            while(element.size()!=0) {
                // if any action needed to perform to display button, please do it.
                element = driver.findElements(By.xpath("//button[@id='btn_ok']"));
                if(element.size()!=0) {
                    wait = new FluentWait<WebDriver>((WebDriver) driver).withTimeout(30, TimeUnit.SECONDS).pollingEvery(5,
                            TimeUnit.SECONDS);
                    wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//button[@id='btn_ok']"))));
                    buttonClicked=true;
                    break;  
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buttonClicked;
    }
© www.soinside.com 2019 - 2024. All rights reserved.