如何等待页面加载 selenium htmlunitDriver?
How can I wait a page to load with selenium htmlunitDriver?
我正在为类似于 Ad.fly 的页面开发一个机器人。打开 link 后,我想等待五秒钟让页面加载,然后才会出现要单击的按钮。
我想用 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"
}
}
我想要一个好的申请方法...如果你能帮助我将不胜感激:)
实际上,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;
我正在为类似于 Ad.fly 的页面开发一个机器人。打开 link 后,我想等待五秒钟让页面加载,然后才会出现要单击的按钮。
我想用 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"
}
}
我想要一个好的申请方法...如果你能帮助我将不胜感激:)
实际上,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;