简体   繁体   English

异常:等待时“ xx秒后超时”。直到Selenium WebDriver C#

[英]Exception: “Timed Out After xx seconds” on wait.until Selenium WebDriver C#

I'm trying to figure out how it's possible to wait for a condition in order to login into a web page with Selenium Driver. 我试图弄清楚如何等待条件以便使用Selenium Driver登录到网页。 However it is not as straightforward as it may seem. 但是,它并不像看起来那样简单。 I'm working around with Thread.Sleep(3000); 我正在研究Thread.Sleep(3000); but I'm sure there should be a better solution. 但我相信应该有更好的解决方案。 So, my code works as follows: 因此,我的代码如下:

  1. Load page with firefox driver. 用firefox驱动程序加载页面。
  2. Execute a javascript snnipet to change language (I need to wait for this in order to login). 执行一个javascript代码片段以更改语言(我需要等待此时间才能登录)。

     IJavaScriptExecutor executor = (IJavaScriptExecutor)firefox; executor.ExecuteScript("arguments[0].click();", idioma_español); 
  3. Above instruction leads to a page reload. 以上说明导致页面重新加载。

  4. Next instruction is intented to wait for page to be reloaded 下一条指令旨在等待页面重新加载

     WebDriverWait wait = new WebDriverWait(newDriver,TimeSpan.FromSeconds(10)); wait.Until(ExpectedConditions.TextToBePresentInElementValue(element,textToAppear)); 
  5. Continue to login. 继续登录。

However, when I run the code, it throws the following exception: 但是,当我运行代码时,它将引发以下异常:

在此处输入图片说明

Looking closer into de output, I found this: 仔细查看de输出,我发现了这一点:

在此处输入图片说明

在此处输入图片说明

I tried with different expected conditions such as: TextToBePresentInElement , ElementExists ; 我试着用不同的预期条件,如: TextToBePresentInElementElementExists ; but it throws the same exception. 但会引发相同的异常。

I also tried with ExecuteAsyncScript("arguments[0].click();", idioma_español); 我还尝试了ExecuteAsyncScript("arguments[0].click();", idioma_español); method, but it throws "Document was unloaded " exception. 方法,但会引发“文档已卸载”异常。

It looks like the text element has been replaced, so the element was stale. 看起来text元素已被替换,因此该元素已过时。 I think you can solve it by fetch the new element every time. 我认为您可以通过每次获取新元素来解决它。 See code below: 参见下面的代码:

public bool WaitForTextToAppear(IWebDriver driver, By elementLocator, string textToAppear, int timeoutInSec=10)
{
    IWait<IWebDriver> wait = new DefaultWait<IWebDriver>(driver);
    wait.Timeout = TimeSpan.FromSeconds(timeoutInSec);
    wait.PollingInterval = TimeSpan.FromMilliseconds(300);
    try
    {
        wait.Until(d => IsTextPresentedIn(d, elementLocator, textToAppear));
        return true;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Error: " + ex.Message);
        return false;
    }
}

private bool IsTextPresentedIn(IWebDriver driver, By elementLocator, string textToAppear)
{
    try
    {
        var elements = driver.FindElements(elementLocator);
        if (elements.Count>0 && elements[0].Text.Equals(textToAppear, StringComparison.OrdinalIgnoreCase))
            return true;
    }
    catch
    {
        return false;
    }
    return false;
}
// using the web driver extension.
bool textAppeared = WaitForTextToAppear(driver, By.CssSelector("selector-of-the-text-element"), "HERE IS THE EXPECTED TEXT");

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

相关问题 C# Selenium:使用等待时无法捕获异常。直到 - C# Selenium: Can't catch exception when using wait.Until chromeDriver窗口在wait.until()C#硒期间崩溃 - chromeDriver window crushes during wait.until() C# selenium 如何将Wait.Until与Selenium C#一起用于现有元素 - How to use Wait.Until with Selenium C# for existing element Selenium WebDriver 异常 HTTP 对远程 WebDriver 服务器的请求 URL xyz x 秒后超时 - Selenium WebDriver Exception The HTTP request to the remote WebDriver server for URL xyz Timed out after x seconds Selenium 3.8.0 wait.until调用引发异常 - Selenium 3.8.0 wait.until call throws exception Selenium C#wait.until(expectedconditions)…函数无法在屏幕上找到对象/元素 - Selenium c# wait.until(expectedconditions)… function fails to find objects/elements on screen C#硒对URL的远程Web驱动程序服务器的HTTP请求在60秒后超时 - C# selenium HTTP request to the remote web driver server for URL timed out after 60 seconds Selenium C# WebDriver:等到元素出现 - Selenium C# WebDriver: Wait until element is present 如何等到元素显示? selenium 网络驱动程序 C# - How to wait until element is shown? selenium webdriver C# Selenium ChromeDriver - 对远程 WebDriver 服务器的 HTTP 请求在 60 秒后超时 - Selenium ChromeDriver - the HTTP request to the remote WebDriver server for URL timed out after 60 seconds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM