当前位置: 首页 > 知识库问答 >
问题:

Selenium SafariDriver不等待页面加载?

李法
2023-03-14

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();
    }
}

如果您使用ChromeDriverFirefoxDriver运行此功能,它将正常运行,即搜索“您好世界”,然后在结果页面上转到图像结果。

使用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)

找不到的元素是“Images”,因为页面在运行该语句之前没有加载。

这是预期行为吗?我应该是狩猎旅行的特例吗?

共有2个答案

阳宗清
2023-03-14

基本上,当您尝试“单击”或“发送密钥”到页面上尚不存在的内容时,Selenium就会爆炸。
(我肯定有默认的隐式等待,但我不确定它是什么)。基本上,这取决于您是否希望测试在失败前等待的灵活性。我希望这能有所帮助。

显式等待示例:

@Test
public void testGoogleSearch() {
    driver.get("http://duckduckgo.com");
    By searchInputBy = By.id("search_form_input_homepage");
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(searchInputBy));
    driver.findElement(searchInputBy).sendKeys("Hello World");
    driver.findElement(By.id("search_button_homepage")).click();
    wait = new WebDriverWait(driver, 10);
    By imagesBy = By.linkText("Images");
    wait.until(ExpectedConditions.elementToBeClickable(imagesBy));
    driver.findElement(imagesBy).click();
} 

隐式等待示例:

@Test
public void testGoogleSearch() {
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
    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();
}

您还可以选择使用fluent waitis,这使您能够更好地控制显式等待,因此您可以告诉它忽略某些异常,但它们更加冗长。

我认为创建一个静态方法库来完成繁重的fluent等待是编写测试的更好、更可读、更不易出错的方法。

FluentWait Javadoc

还有一个很好的答案,更详细地解释了等待。

赵雅懿
2023-03-14

根本原因是你的代码没有等待搜索结果。您可以使用< code>WebDriverWait和< code>ExpectedConditions来等待< code>images链接。参见下面的示例。

@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();
    WebDriverWait waiter = new WebDriverWait(driver,20);
    WebElement imagesLink = waiter.until(ExpectedConditions.elementToBeClickable(By.linkText("Images")));
    imagesLink.click();
}
 类似资料:
  • 我试图创建一个等待加载页面的方法,但我出现了一个错误。可能我没有正确使用这个方法。 错误是:

  • 本文向大家介绍jQuery EasyUI 页面加载等待及页面等待层,包括了jQuery EasyUI 页面加载等待及页面等待层的使用技巧和注意事项,需要的朋友参考一下 下面一个代码片段是 easyUI 页面加载等待,代码如下所示: 下面看个工具类 基于easyui的页面等待提示层,即mask 效果图: 以上所述是小编给大家介绍的jQuery EasyUI 页面加载等待及页面等待层,希望对大家有所帮

  • 我对selenium webdriver有问题,如果有人能帮助我,我将非常感激 环境: 硒服务器独立-2.31.0.jar / 硒-服务器-独立-2.35.0.jar IE驱动程序服务器.exe(已尝试版本 2.28 - 2.35) 示例代码: 问题:修复这些问题都会对我有所帮助 href。sendKeys()成功模拟用户单击,但不等待页面加载。 href。click()无法模拟用户单击,但成功等

  • 问题内容: 我编写了一个脚本,该脚本从页面中获取数据,但是有时页面需要花费一些时间来加载,因此当将html拉到汤对象中时,有时它什么也不会拉,因为页面仍然需要完成。 我编写了以下代码以等待页面完成。 有用 但是调用函数时出现以下错误; 问题答案: 我认为您应该这样使用: 如手册所述。

  • 问题内容: 使用Selenium2Driver将Behat与Mink结合使用时,是否有办法可靠地等待页面加载? 我继承了一些旧的测试,像这样等待: 即仅此一次测试就需要1分钟30秒。 我想做的是有一种通用的方式来等待上一次单击以导致页面加载,而不必每次都等待大量固定的时间。 我可以看到等待页面加载的所有建议,都涉及检查是否已加载特定页面元素。 但是,这些测试是针对各种传统网站运行的,这些网站中并不

  • 问题内容: 您如何使Selenium 2.0等待页面加载? 问题答案: 您还可以使用以下代码检查页面加载