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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21214340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:23:01  来源:igfitidea点击:

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

javaseleniumselenium-webdriver

提问by roosevelt

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. Page2 starts loading... but the problem is, Page2 uses Google Analytics codes, and sometimes it takes forever for the page to stop loading.

我正在创建一个测试并遇到一些问题。这是场景。我使用 Selenium Web 驱动程序在 Page1 上填写表单并通过单击按钮提交表单。Page2 开始加载...但问题是,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!

临时解决方案:向下滚动以获取答案!

回答by Buddha

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

回答by ucsunil

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. This works for me when I work with feature rich pages.

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

回答by Akbar

Instead of using the webdriver click() to submit the form use jsexecutor and do a click. Jsexecutor does not wait for page load and you can with other actions.

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

回答by roosevelt

So, I reported to Selenium about these issues. And the temporary workaround is... messing with Firefox's timeout settings. Basically by default Firefox waits about 250 seconds for each connection before timing you out. You can check about:config for the details. 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.

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

Similar config might exist for other browsers. I still think Selenium should let us handle the pagetimeout exception. 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.

其他浏览器可能存在类似的配置。我仍然认为 Selenium 应该让我们处理 pagetimeout 异常。确保在此处为错误添加星标:http: //code.google.com/p/selenium/issues/detail?id=6867&sort=-id&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner% 20Summary,所以 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();

回答by user3227421

As per the above scenario explained i feel its best to use the below wait command in the first page.

根据上述情况的解释,我觉得最好在第一页中使用以下等待命令。

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.

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

回答by user2886114

As per the above scenario explained i feel its best to use the below wait command in the first page.

根据上述情况的解释,我觉得最好在第一页中使用以下等待命令。

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.

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

回答by SATYA

Use explicit/webdriver wait----

使用显式/网络驱动等待----

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