如果已加载所需元素,请使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); 

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

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

其他浏览器可能存在类似的配置。 我仍然认为Selenium应该让我们处理pagetimeoutexception。 请务必在此处为错误添加星标: 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(); 

下面给出一个镜头。

 driver.findElement(By.tagName("body")).sendKeys("Keys.ESCAPE"); 

要么

 ((JavascriptExecutor) driver).executeScript("return window.stop"); 

或者,您也可以使用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();"); } 

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

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

一旦检查了元素并且您知道它存在,您可以导航到/加载不同的页面(如果下一个任务在不同的页面上)或者任务是在同一页面上(就像你做的那样)不需要尚未加载的元素),你可以像往常一样继续 – selenium将识别已经加载的元素。 当我使用function丰富的页面时,这对我有用。

而不是使用webdriver click()提交表单使用jsexecutor并单击。 Jsexecutor不会等待页面加载,您可以使用其他操作。

根据上面描述的场景,我觉得最好在第一页中使用下面的wait命令。

 WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>))); 

在第一页中找到所需元素后,您可以进入第二页。

根据上面描述的场景,我觉得最好在第一页中使用下面的wait命令。

 WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>))); 

在第一页中找到所需元素后,您可以进入第二页。

使用explicit / webdriver等—-

  WebDriverWait wt=new WebDriverWait(driver, 20); wt.until(ExpectedConditions.elementToBeClickable(By.name("abc")));