猿问

检查按钮是否可用?如果不等待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">


烙印99
浏览 107回答 4
4回答

慕的地10843

您可以使用 Fluent wait 来执行此操作。这将检查按钮是否每5秒可单击30秒。您可以根据需要调整时间。尝试此代码并提供反馈,无论它是否有效。&nbsp;Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .withTimeout(30, TimeUnit.SECONDS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .pollingEvery(5, TimeUnit.SECONDS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ignoring(NoSuchElementException.class);&nbsp; &nbsp; &nbsp; &nbsp; WebElement clickseleniumlink = wait.until(new Function<WebDriver, WebElement>(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public WebElement apply(WebDriver driver ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return driver.findElement(By.xpath("//button[@id='btn_ok']"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; clickseleniumlink.click();

qq_遁去的一_1

试试这个方法。看看是否有帮助。int size=driver.findElements(By.xpath("//button[@id='btn_ok']")).size();if (size>0)&nbsp; {&nbsp; &nbsp; driver.findElement(By.xpath("//button[@id='btn_ok']")).click();&nbsp; }&nbsp; else&nbsp; {&nbsp; &nbsp; driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);&nbsp; &nbsp; int size1=driver.findElements(By.xpath("//button[@id='btn_ok']")).size();&nbsp; &nbsp; if (size1>0)&nbsp; {&nbsp; &nbsp; driver.findElement(By.xpath("//button[@id='btn_ok']")).click();&nbsp; }}&nbsp; &nbsp;

守着星空守着你

您可以使用显式等待来等待按钮可单击。它将每500毫秒测试一次按钮,最长为指定时间,直到它可点击WebDriverWait wait = new WebDriverWait(driver, 5); // maximum wait time is 5 here, can be set to longer timeWebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("btn_ok")));button.click();作为旁注,设置驱动程序将搜索元素的最长时间,它不会延迟脚本。implicitlyWait

慕神8447489

我更喜欢这个,因为它可以采取任何布尔条件来“等到”。&nbsp; &nbsp; public static void WaitUntil(this IWebDriver driver, Func<bool> Condition, float timeout = 10f)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; float timer = timeout;&nbsp; &nbsp; &nbsp; &nbsp; while (!Condition.Invoke() && timer > 0f) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Threading.Thread.Sleep(500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer -= 0.5f;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.Threading.Thread.Sleep(500);&nbsp; &nbsp; }&nbsp; &nbsp; driver.WaitUntil(() => driver.FindElements(By.XPath("some xpath...").Length == 0);&nbsp; &nbsp; //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.&nbsp; &nbsp; driver.WaitUntil(() => "Something exists in the database");我发现隐含的等待给我带来的麻烦比它的价值更大。我发现显式硒等待可能会变得有点冗长,并且它并没有在我的框架中涵盖我需要的所有内容,所以我已经进行了大量的扩展。这是其中之一。请注意,我在上面的示例中使用FindElements,因为我不希望在未找到任何内容时引发异常。这应该适合您。注意:这是C#,但是对于任何语言(尤其是Java)修改它应该不难。如果您的语言不允许这样的扩展,只需在类中直接调用该方法即可。您需要将其放在静态类中才能正常工作。在逻辑中像这样扩展现有类时要小心,因为当其他人试图确定定义方法的位置时,它可能会混淆。
随时随地看视频慕课网APP

相关分类

Java
我要回答