我们是否有任何通用功能来检查页面是否已完全加载到Selenium中 [英] Do we have any generic function to check if page has completely loaded in Selenium
问题描述
我正在尝试检查硒中的网页加载是否完成(即检查所有控件是否已加载).
我尝试了以下代码:
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
但是即使页面正在加载上面的代码也不会等待.
我知道我可以检查特定元素以检查其可见/可点击等,但是我正在寻找一些通用解决方案
正如您所提到的,如果有任何通用功能可以检查页面是否已通过 上面代码行中的参数 现在,由于您的用例与页面已完全加载有关,您可以使用
使用 ChromeOptions 类: 您可以在的 您可以在仅限IE IE WebDriver中找到详细的讨论在调试时有效 但是浏览器客户端达到 要等待所有 JavaScript 和 Ajax调用完成,可以编写以下函数: 您可以在>等待ajax请求完成后找到详细的讨论-硒webdriver 现在,通过 PageLoadStrategy 和
现在,有时页面标题可能与您的应用程序标题相匹配,但您想要交互的所需元素尚未完成加载.因此,更精细的方法是诱使 ExpectedConditions 设置为
您可以在以下位置找到几个相关的详细讨论: I am trying to check if web page is loaded completed or not (i.e. checking that all the control is loaded) in selenium. I tried below code: but even if page is loading above code does not wait. I know that I can check for particular element to check if its visible/clickable etc but I am looking for some generic solution As you mentioned if there is any generic function to check if the page has completely loaded through Selenium the answer is No. First let us have a look at your code trial which is as follows : The parameter Here you can find a detailed discussion of pageLoadTimeout in Selenium not working Now as your usecase relates to page being completely loaded you can use the pageLoadStrategy() set to Using DesiredCapabilities Class : Using ChromeOptions Class : You can find a detailed discussion in Page load strategy for Chrome driver (Updated till Selenium v3.12.0) Now setting PageLoadStrategy to NORMAL and your code trial both ensures that the Browser Client have (i.e. the Web Browser) have attained You can find a detailed discussion in Selenium IE WebDriver only works while debugging But the Browser Client attaining To wait for the all the JavaScript and Ajax Calls to complete you can write a function as follows : You can find a detailed discussion in Wait for ajax request to complete - selenium webdriver Now, the above two approaches through PageLoadStrategy and
Now, at times it is possible though the Page Title will match your Application Title still the desired element you want to interact haven't completed loading. So a more granular approach would be to induce WebDriverWait inconjunction with ExpectedConditions set to visibilityOfElementLocated() method which will make your program wait for the desired element to be visible as follows :
You can find a couple of relevant detailed discussions in: 这篇关于我们是否有任何通用功能来检查页面是否已完全加载到Selenium中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!pageLoadTimeout
并没有真正转换为实际的 Selenium中的pageLoadTimeout无法正常工作的详细讨论 >
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.PageLoadStrategy;
public class myDemo
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
FirefoxOptions opt = new FirefoxOptions();
opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
driver.quit();
}
}
'document.readyState'
.一旦满足此条件,Selenium将执行下一行代码.'document.readyState'
等于"complete"
仍然不能保证所有 JavaScript 和 Ajax调用完成.
public void WaitForAjax2Complete() throws InterruptedException
{
while (true)
{
if ((Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0")){
break;
}
Thread.sleep(100);
}
}
"return jQuery.active == 0"
的上述两种方法似乎正在等待不确定的事件.因此,可以等待一段时间,以诱导 ExpectedConditions 设置为
参考文献
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
pageLoadTimeout
in the above line of code doesn't really reseambles to actual pageLoadTimeout().
normal
[ the supported values being none, eager or normal ] using either through an instance of DesiredCapabilities Class or ChromeOptions Class as follows :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class myDemo
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
DesiredCapabilities dcap = new DesiredCapabilities();
dcap.setCapability("pageLoadStrategy", "normal");
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dcap);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
driver.quit();
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.PageLoadStrategy;
public class myDemo
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
FirefoxOptions opt = new FirefoxOptions();
opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
driver.quit();
}
}
'document.readyState'
equal to "complete"
. Once this condition is fulfilled Selenium performs the next line of code.'document.readyState'
equal to "complete"
still doesn't guarantees that all the JavaScript and Ajax Calls have completed.
public void WaitForAjax2Complete() throws InterruptedException
{
while (true)
{
if ((Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0")){
break;
}
Thread.sleep(100);
}
}
"return jQuery.active == 0"
looks to be waiting for indefinite events. So for a definite wait you can induce WebDriverWait inconjunction with ExpectedConditions set to titleContains() method which will ensure that the Page Title (i.e. the Web Page) is visible and assume the all the elements are also visible as follows :driver.get("https://www.google.com/");
new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("partial_title_of_application_under_test"));
System.out.println(driver.getTitle());
driver.quit();
driver.get("https://www.google.com/");
WebElement ele = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_of_the_desired_element")));
System.out.println(ele.getText());
driver.quit();
References