selenium - Webdriver(Selenium 2) - 如何让selenium操作元素而无需等待连接到外部AD链接?

标签 selenium selenium-webdriver phpunit

环境:
- Selenium 2.39 独立服务器
- PHP 5.4.11
- PHPUnit 3.7.28
- Chrome V31 和 ChromeDriver v2.7

我正在测试一个网站,该网站调用了很多广告系统,例如Google AD。
即使页面的所有元素都已加载,浏览器仍需要花费大量时间来连接外部 AD 链接。

如果当我在网页上运行测试时我的互联网网络速度不快, Selenium 会等待很长时间,因为 AD 链接响应缓慢。
在这种情况下,Selenium通常会等待60秒以上,并抛出超时异常。

我不确定Senelium是如何工作的,但似乎Selenium必须等待网页完全加载的迹象,然后拉动DOM来查找元素。

我想让selenium操作元素而无需等待连接到外部AD链接。
有没有办法做到这一点 ?非常感谢。

最佳答案

我建议您可以使用代理。 Browsermob与 selenium 集成良好,非常易于使用:

// start the proxy
ProxyServer server = new ProxyServer(4444);
server.start();

// get the Selenium proxy object
Proxy proxy = server.seleniumProxy();

// This line will automatically return http.200 for any request going to google analytics
server.blacklistRequests("https?://.*\\.google-analytics\\.com/.*", 200);

// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);

// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);

I'm not sure how Senelium works, but it seems that Selenium has to wait for a sign of webpage's full loading, then pulls the DOM to find elements.

差不多就是这个样子。默认loading strategy是“正常”,这意味着:

NORMAL of type DOMString The remote end MUST wait until the "document.readyState" of the frame currently handling commands equals "complete", or there are no more outstanding network requests other than XMLHttpRequests.

关于selenium - Webdriver(Selenium 2) - 如何让selenium操作元素而无需等待连接到外部AD链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22218735/

相关文章:

java - Webdriver,检测DOM变化并等待

java - 拆解未初始化

python Selenium : click() cannot trigger event

javascript - selenium.common.exceptions.WebDriverException : Message: unknown error: 'script' must be a string while using execute_script() through Selenium Python

java - 在 Java 中使用 Selenium 2 将鼠标悬停在 WebElement 上

java - 如何使用类获取输入值?

PHPUnit 用 stub 覆盖实际方法

phpunit - 如何安装 PHPUnit 3.5 而不是 3.6?

phpunit - 如何在 Codeception 功能测试中使用 PHPUnit 断言方法?

java - 我如何验证 mer. 2019 年 5 月 1 日按钮?