等待页面加载 [英] Wait for page to load

查看:75
本文介绍了等待页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一种方法,以等待javascript加载页面,但是出现错误.可能我没有正确使用该方法.

I'm trying to create a method that wait for the page to load by javascript, but I'm having an error. probably I'm not using the method correctly.

public static void WaitForLoad(this IWebDriver driver, int timeoutSec = 15)
    {
        WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, timeoutSec));
        wait.Until(wd => wd.ExecuteJavaScript("return document.readyState") == "complete");
    }

错误是:

The type arguments for method 'WebdriverExtensions.ExecuteJavaScript<T>(IWerbDriver,string,params object[]' cannot be inferred from the usage.Try specifying the type arguments explicity)

推荐答案

WaitForLoad(...)函数中,您正在检查("return document.readyState") == "complete"

In the WaitForLoad(...) function you are checking for ("return document.readyState") == "complete"

实际上,对函数WaitForLoad(...)的调用纯粹是重载,没有实际作用.这是因为 Selenium DLL/JARS/Modules 的设计方式是,仅当您使用的 Browser 发送document.readyState == "complete" WebDriver .

Practically, the call to the function WaitForLoad(...) is purely an overload and have no real effect. That is because Selenium DLL/JARS/Modules are designed in such a way that the webdriver executes the next line of code only when the Browser which you are using sends document.readyState == "complete" to the WebDriver.

在您的情况下,如果我们等待而不是验证document.readyState == "complete",则效果会好得多,例如,为下一个 WebElement 引入 ExplicitWait 我们打算与ElementIsVisible(By)ElementToBeClickable(By)ElementToBeClickable(IWebElement)ElementToBeSelected(IWebElement, Boolean)等中的适当子句进行交互.

In your case instead of validating document.readyState == "complete" it will be much more effective if we wait i.e. induce ExplicitWait for the next WebElement with which we intend to interact with appropriate clause from either ElementIsVisible(By), ElementToBeClickable(By), ElementToBeClickable(IWebElement), ElementToBeSelected(IWebElement, Boolean), etc.

这篇关于等待页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆