简体   繁体   English

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

[英]Make Selenium Webdriver Stop Loading the page if the desired element is already loaded?

I am creating a test and having some issues. 我正在创建一个测试并遇到一些问题。 Here is the scenario. 这是场景。 I use Selenium Web driver to fill out a form on Page1 and submit the form by clicking a button. 我使用Selenium Web驱动程序在Page1上填写表单,然后单击按钮提交表单。 Page2 starts loading... but the problem is, Page2 uses Google Analytics codes, and sometimes it takes forever for the page to stop loading. 第2页开始加载...但问题是,Page2使用Google Analytics代码,有时页面停止加载需要一整年。

Even though the expected element is already present, Selenium web driver does not proceed until the whole web page is fully loaded. 即使预期的元素已经存在,Selenium Web驱动程序也不会继续,直到整个网页完全加载。

How do I make Selenium to move on to the next task or stop loading external javascript/css if the expected element is already present? 如果预期元素已存在,如何让Selenium继续下一个任务或停止加载外部javascript / css?

I tried tweaking the following settings but no luck. 我尝试调整以下设置但没有运气。

driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

TEMPORARY SOLUTION: Scroll below for answer! 临时解决方案:滚动下面的答案!

Give below approaches a shot. 下面给出一个镜头。

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

or 要么

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

Alternatively, you can also use WebDriverBackedSelenium as shown in the snippet below from Vincent Bouvier. 或者,您也可以使用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();");
}

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

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

So, I reported to Selenium about these issues. 所以,我向Selenium报告了这些问题。 And the temporary workaround is... messing with Firefox's timeout settings. 临时解决方法是......搞乱Firefox的超时设置。 Basically by default Firefox waits about 250 seconds for each connection before timing you out. 基本上默认情况下,Firefox会在您计时之前等待大约250秒。 You can check about:config for the details. 您可以查看:config以获取详细信息。 Basically I cranked it down so Firefox doesn't wait too long and Selenium can continue as if the page has already finished loading :P. 基本上我把它调低了,所以Firefox不会等待太长时间,Selenium可以继续,好像页面已经完成加载:P。

Similar config might exist for other browsers. 其他浏览器可能存在类似的配置。 I still think Selenium should let us handle the pagetimeout exception. 我仍然认为Selenium应该让我们处理pagetimeout异常。 Make sure you add a star to the bug here: http://code.google.com/p/selenium/issues/detail?id=6867&sort=-id&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary , so selenium fixes these issues. 请务必在此处为错误添加星标: 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();

Once you have checked for the element and you know that it is present, you could either navigate to/load a different page (if the next tasks are on a different page) or if the tasks are on the same page (as you anyway do not need the elements that have not yet loaded), you could continue as usual - selenium will identify the elements which have already been loaded. 一旦检查了元素并且您知道它存在,您可以导航到/加载不同的页面(如果下一个任务在不同的页面上)或者任务是在同一页面上(就像你做的那样)不需要尚未加载的元素),你可以像往常一样继续 - selenium将识别已经加载的元素。 This works for me when I work with feature rich pages. 当我使用功能丰富的页面时,这对我有用。

Instead of using the webdriver click() to submit the form use jsexecutor and do a click. 而不是使用webdriver click()提交表单使用jsexecutor并单击。 Jsexecutor does not wait for page load and you can with other actions. Jsexecutor不会等待页面加载,您可以使用其他操作。

As per the above scenario explained i feel its best to use the below wait command in the first page. 根据上面解释的场景,我觉得最好在第一页中使用下面的wait命令。

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

Once the required element is found in the first page next you can proceed to the second page. 在第一页中找到所需元素后,您可以继续第二页。

As per the above scenario explained i feel its best to use the below wait command in the first page. 根据上面解释的场景,我觉得最好在第一页中使用下面的wait命令。

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

Once the required element is found in the first page next you can proceed to the second page. 在第一页中找到所需元素后,您可以继续第二页。

Use explicit/webdriver wait---- 使用explicit / webdriver等----

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM