2014-02-23 134 views
4

我们的一些页面在后端运行第三方分析。在我看来,硒正在等待这些过程完成,然后再进入下一步。问题是它需要大约5-6分钟。有没有什么方法可以等待并进入下一步?如何让Selenium WebDriver不要等待页面的完整加载

我试着用JavaScript单击按钮(将用户带到具有一些后端进程的页面),然后它就卡住了下一步。

谢谢。

+0

这就是我的问题了。没有几分钟,但我的测试仍然需要很长时间。即使浏览器仍然可用,WebDriver将等待完整的页面加载。我知道这对大多数情况都很有用,但就我而言,该页面只是一个“浏览页面”,会让我感到沮丧。 – KFleischer

回答

3

在页面加载时单击元素甚至可以在webdriver上;默认情况下,webdriver等待整个页面加载,然后选择元素。链接和文本可见,但不可点击;但是,在页面加载时,它在Selenium IDE拾取元素上运行良好。

Webdriver利用FirefoxProfile来避免此类风险;它仅适用于Firefox浏览器。

FirefoxProfile fp = new FirefoxProfile(); 
fp.setPreference("webdriver.load.strategy", "unstable"); 
driver = new FirefoxDriver(fp); 
0

我建议你可以使用代理。 Browsermob与硒结合的很好,非常容易使用它:

// 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); 

编辑: 我刚刚发现,在selenium 2.40的pageLoading战略得以实施。

  • 在Firefox中实现pageLoadingStrategy功能。
partial interface WebDriver { 
    const DOMString CONSERVATIVE = 'conservative'; 
    const DOMString NORMAL = 'normal'; 
    const DOMString EAGER = 'eager'; 
    const DOMString NONE = 'none'; 
}; 

reference

+0

您能解释一下如何使用新的pageLoadStrategy功能以及它如何影响硒的使用? – KFleischer

+0

[检查已执行的测试](https://github.com/SeleniumHQ/selenium/blob/master/java/client/test/org/openqa/selenium/PageLoadingTest.java) –

相关问题