如何使用java在selenium中显式地为driver.get()提供等待
如何等待driver.get(),因为我们正在使用.get()访问的URL不知道。而且可能需要未知的时间,所以我们必须给潜水员30秒的超时时间。get()然后如何给它 下面是它的代码如何使用java在selenium中显式地为driver.get()提供等待,java,selenium,webdriver,Java,Selenium,Webdriver,如何等待driver.get(),因为我们正在使用.get()访问的URL不知道。而且可能需要未知的时间,所以我们必须给潜水员30秒的超时时间。get()然后如何给它 下面是它的代码 package org.openqa.selenium.example; import java.util.List; import org.openqa.selenium.By import org.openqa.selenium.WebDriver; import org.openqa.selenium
package org.openqa.selenium.example;
import java.util.List;
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyClass{
public static void main(String[] args) throws Exception {
// The Firefox driver supports javascript
WebDriver driver = new HtmlUnitDriver();
// Go to the some websites
driver.get("http://www.abced.com/123456/asd.htm");
/*** Here we DONT get back the driver, so we need to Give Time out of 30 seconds**/
final List<WebElement> element1= driver.findElements(By.tagName("a"));
for (WebElement webElement : element1) {
String urlFromPage = webElement.getAttribute("href");
System.out.println(urlFromPage);
}
}
}
它不起作用。。
请允许我建议,
tx如果要等待页面加载,则应使用
pageLoadTimeout(长时间,java.util.concurrent.TimeUnit)
方法。隐式wait(longtime,java.util.concurrent.TimeUnit)
用于等待尚未出现的元素,而不是等待页面加载
在WebDriver实例上,应该调用与隐式wait()
使用的方法链类似的方法链。这将依次调用:
-驱动程序管理界面manage()
-驱动程序选项界面options()
-超时选项界面超时()
-将超时设置为所需的时间pageLoadTimeout(…)
您可以找到相关的javadoc。相反,您可以使用WebDriverWait指定要检查的条件和最大超时。这可以按如下方式使用:
WebDriverWait _wait = new WebDriverWait(driver, new TimeSpan(0, 0, 2)); //waits 2 secs max
_wait.Until(d => d.FindElement(By.Id("name")));
我已经发布了一个类似的问题答案。谢谢您的快速回复。我不知道怎么用它。。那么,请您告诉我如何在驱动程序中使用pageLoadTimeout()。。我使用的selenium 16.0经过了更详细的编辑。如果您需要更多帮助,请编辑您的问题,使其更清楚地表明您正在尝试执行的操作。我尝试了相同的操作,但在执行程序时,我遇到异常:“java.lang.UnsupportedOperationException:pageLoadTimeout,因此,我无法处理此问题..您是否已通过源代码追溯到引发异常的位置?是的,我们将在何时设置pageLoadTimeout()
WebDriverWait _wait = new WebDriverWait(driver, new TimeSpan(0, 0, 2)); //waits 2 secs max
_wait.Until(d => d.FindElement(By.Id("name")));