我正在使用selenium,IEDriver和C#,我想等待页面加载。我有这段代码:
/// <summary>
/// Ceka dokud neni stranka nastena
/// </summary>
public static void WaitForPageToLoad()
{
try
{
Thread.Sleep(500);
Log.Trace("Browser.WaitForPageToLoad() - Ceka dokud neni stranka nactena ...");
new WebDriverWait(Browser.Driver, new TimeSpan(0, 0, 360)).Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
Thread.Sleep(1000);
}
catch (Exception ex)
{
Log.Error(ex);
throw;
}
}
但它会崩溃:
2018-01-04 15:39:27.2266 - ERROR: System.InvalidOperationException: JavaScript error (UnexpectedJavaScriptError)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
at BaseFramework.Browser.<>c.<WaitForPageToLoad>b__10_0(IWebDriver d) in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
at BaseFramework.Browser.WaitForPageToLoad() in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
EXCEPTION: System.InvalidOperationException: JavaScript error (UnexpectedJavaScriptError)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
at BaseFramework.Browser.<>c.<WaitForPageToLoad>b__10_0(IWebDriver d) in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
at BaseFramework.Browser.WaitForPageToLoad() in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 103
at Gamma.Tests.GammaICRM.AT82688_ICRM_SMOKE() in C:\TFS\PRIVPMT\Selenium\Gamma.UI.Tests\Gamma.Tests\GammaICRM.cs:line 72
at Gamma.Tests.GammaICRM.AT82688_ICRM_SMOKE_PerformTest() in C:\TFS\PRIVPMT\Selenium\Gamma.UI.Tests\Gamma.Tests\GammaICRM.cs:line 23
大部分时间都有效,但有时它会在此
上崩溃下一个方法是:
public static void LeftClick(this IWebElement element)
{
//pockame dokud nelze na element kliknout
new WebDriverWait(Browser.Driver, new TimeSpan(0, 0, 120)).Until(ExpectedConditions.ElementToBeClickable(element));
Actions actions = new Actions(Browser.Driver);
//posuneme cursor na element
actions.MoveToElement(element).Perform();
//klikeneme na element
actions.Click().Build().Perform();
}
传递webElement是由XPath创建的(webElement总是正确的)
答案 0 :(得分:0)
您可以尝试调用WebDriverWait类。您可以使用它来等待元素显示在页面上(即如果此对象存在则页面加载)
public static IWebElement WaitUntilElementExists(By elementLocator, int timeout = 10)
{
try
{
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
return wait.Until(ExpectedConditions.ElementExists(elementLocator));
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
throw;
}
}
在此代码中,该方法返回wait.until,然后对应该在页面上的元素上的elementexists条件。只是让它尝试在页面加载时在页面上找到一个应该存在的元素。如果它在一段时间内不存在,它将给出NoSuchElementException(因此超时)。抓住这个并给出你自己的输出
来源:How to get webDriver to wait for page to load (C# Selenium project)
希望这对所有人有帮助
答案 1 :(得分:0)
我遇到了同样的问题,问题是我在框架内。在执行此javascript之前切换回默认内容可以解决该错误。
希望问题仍然对您有帮助。
总之,在致电login(email: string, password: string): Observable<Response> {
return this.httpClient.post(url, {email: email, password: password}).pipe(
tap(async (response: Response) => {
// login successful if there's a jwt token in the response
if (response) {
this.token = response.token;
}
return response;
})
);
}
之前,我已经使用过WaitForPageToLoad()