2

我正在为类似于 Ad.fly 的页面开发机器人。打开链接后,我想等待五秒钟让页面加载,然后单击按钮出现。

我想用HtmlunitDriver. 我尝试了隐式等待和显式等待,但这不起作用。有人告诉我使用FluentWait,但我不知道如何实现它。

这是我的实际代码,有人可以帮我理解如何实现FluentWait吗?

public class bot {

public static WebDriver driver;
public static void main(String[] args) {
     driver = HtmlUnitDriver();
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     driver.get("http://bc.vc/xHdGKN");
     // HERE I HAVE TO USE FLUENT WAIT, SOMEBODY MAY EXPLAIN TO ME?
     driver.findElement(By.id("skip_btn")).click(); // element what i have to do click when the page load 5 seconds "skip ads button"
}

}

我想要一个好的申请方法...如果您能提供帮助,我将不胜感激:)

4

1 回答 1

1

实际上,FluentWait更适合等待范围很广的情况,比如说 1 到 10 秒之间的任何时间。例如:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(10, TimeUnit.SECONDS)
        .pollingEvery(1, TimeUnit.SECONDS)
        .ignoring(NoSuchElementException.class);

WebElement el = wait.until(new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
        return driver.findElement(By.id("skip_btn"));
    }
});

el.click();

可以肯定的是,这些是您需要的导入语句:

import com.google.common.base.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import java.util.concurrent.TimeUnit;
于 2015-07-13T19:53:31.967 回答