如果已加载所需元素,请使Selenium Webdriver停止加载页面?

roo*_*elt 7 java selenium selenium-webdriver

我正在创建一个测试并遇到一些问题.这是场景.我使用Selenium Web驱动程序在Page1上填写表单,然后单击按钮提交表单.第2页开始加载...但问题是,Page2使用Google Analytics代码,有时页面停止加载需要一整年.

即使预期的元素已经存在,Selenium Web驱动程序也不会继续,直到整个网页完全加载.

如果预期元素已存在,如何让Selenium继续下一个任务或停止加载外部javascript/css?

我尝试调整以下设置但没有运气.

driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

临时解决方案:滚动下面的答案!

Bud*_*dha 5

下面给出一个尝试。

driver.findElement(By.tagName("body")).sendKeys("Keys.ESCAPE");
Run Code Online (Sandbox Code Playgroud)

要么

((JavascriptExecutor) driver).executeScript("return window.stop");
Run Code Online (Sandbox Code Playgroud)

另外,您也可以使用WebDriverBackedSelenium,如下面Vincent Bouvier的片段所示。

//When creating a new browser:
WebDriver driver = _initBrowser(); //Just returns firefox WebDriver
WebDriverBackedSelenium backedSelenuium = 
            new WebDriverBackedSelenium(driver,"about:blank");    

//This code has to be put where a TimeOut is detected
//I use ExecutorService and Future<?> Object

void onTimeOut()
{
    backedSelenuium.runScript("window.stop();");
}
Run Code Online (Sandbox Code Playgroud)

资料来源:https : //sqa.stackexchange.com/a/6355

资料来源:https : //stackoverflow.com/a/13749867/330325


roo*_*elt 5

所以,我向Selenium报告了这些问题.临时解决方法是......搞乱Firefox的超时设置.基本上默认情况下,Firefox会在您计时之前等待大约250秒.您可以查看:config以获取详细信息.基本上我把它调低了,所以Firefox不会等待太长时间,Selenium可以继续,好像页面已经完成加载:P.

其他浏览器可能存在类似的配置.我仍然认为Selenium应该让我们处理pagetimeout异常.请务必在此处为错误添加星标:http://code.google.com/p/selenium/issues/detail?id = 6867&sort = -id&cospecpec = ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner% 20总结,所以selenium解决了这些问题.

FirefoxBinary firefox = new FirefoxBinary(new File("/path/to/firefox.exe"));
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setAcceptUntrustedCertificates(true);
customProfile.setPreference("network.http.connection-timeout", 10);
customProfile.setPreference("network.http.connection-retry-timeout", 10);

driver = new FirefoxDriver(firefox, customProfile);
driver.manage().deleteAllCookies();
Run Code Online (Sandbox Code Playgroud)