看来Selenium的SafariDriver不需要等待网页的加载。我的测试如下:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.safari.SafariDriver;
public class SafariTest {
private WebDriver driver;
@Before
public void setUp() throws Exception {
driver = new SafariDriver();
}
@After
public void tearDown() throws Exception {
driver.close();
}
@Test
public void testGoogleSearch() {
driver.get("http://duckduckgo.com");
driver.findElement(By.id("search_form_input_homepage")).sendKeys("Hello World");
driver.findElement(By.id("search_button_homepage")).click();
driver.findElement(By.linkText("Images")).click();
}
}如果您使用ChromeDriver (或FirefoxDriver )运行此操作,它将按应有的方式运行,即搜索"Hello“,然后在结果页面上转到图像结果。
对于SafariDriver,它在以下方面失败:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)无法找到的元素是“图片”,因为页面在运行该语句之前还没有加载。
发布于 2017-08-02 21:59:35
您必须在WebDriverWait中使用一个显式等待来说明这一点:
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
WebElement images = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Images")));
images.click();https://sqa.stackexchange.com/questions/28889
复制相似问题