简体   繁体   English

C#Selenium Webdriver如何获取当前超时时间

[英]C# selenium Webdriver how to get the current timeout time

I want to create the equivalent of a wait for element present command with custom timeout time 我想创建一个具有自定义超时时间的waiting element present命令的等效项

public static void WaitForElementPresent(string Xpath, int WaitTime, int OriginalWaitTime)
{
    try
    {
        SeleniumBrowser.driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(WaitTime));
        SeleniumBrowser.driver.FindElements(By.XPath(Xpath));
        SeleniumBrowser.driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(OriginalWaitTime));
    }
    catch (Exception)
    {
        throw new Exception("Could not find the" + Xpath + "Xpath after" + DefaultWaitTime + "second");
    }
} 

So far my code looks like this however instead of setting a hard coded integer at the end id rather get the previous timeout time registered as an integer mid test from some obscure function and call it so to reset the timeout to its previous time. 到目前为止,我的代码看起来像这样,而不是在结尾id处设置一个硬编码整数,而不是从一些晦涩的函数中将先前的超时时间注册为整数中间测试,然后调用它以将超时重置为先前的时间。

You should avoid those Implicit time outs as much as possible and stick to explicit waiting. 您应尽可能避免那些隐式超时,并坚持明确等待。 Explicit waiting is not Thread.Sleep(time) but explicitly waiting for your element. 显式等待不是Thread.Sleep(time),而是显式等待您的元素。

A huge lesson came in realizing how Implicit and Explicit waiting work. 了解隐式和显式等待的工作原理是一个巨大的教训。 One thing to note is that C# will look for the Element immediately and when it is not found will crash with a "No Such Element" exception. 需要注意的一件事是C#将立即查找Element,当找不到该元素时,它将崩溃并显示“ No Such Element”异常。 If you mix implicit and explicit waiting together you will be stuck waiting until the Implicit wait time completes even if the element has been found. 如果将隐式等待和显式等待混合在一起,即使隐式等待时间完成,即使找到了元素,您也将一直处于等待状态。 Not good and caused huge time issues for tests. 不好,并且导致测试的大量时间问题。

I would not recommend using XPath unless needed, however, you should use one of the following ways to wait. 除非需要,否则我不建议您使用XPath,但是,您应该使用以下一种方式进行等待。 This is mostly handled with Selenium code already. 这大部分已经用硒代码处理了。

I personally extended Selenium to do the following call with set Page Objects 我亲自扩展了Selenium,以使用设置的页面对象进行以下调用

Expected Condition way: 预期条件方式:

public static void WaitForElement(this IWebElement element, IWebDriver 
 driver, int waitTime)
        {            
            try{                
                 var wait = new WebDriverWait(driver, timeSpan.FromSeconds(waitTime));
                 wait.Until(ExpectedConditions.ElementToBeClickable(element));
                 Logging.Information(elementText + " is clickable");
               }
               catch (NoSuchElementException)
               {
                    throw new Exception("Could not find the" + Xpath + "Xpath after" + DefaultWaitTime + "second");
               }
            }
        }

This would look like this with a mapped page object: 对于映射的页面对象,它看起来像这样:

SaveButton.WaitForElement(driver);

I also extended selenium to use the same waiting for unmapped Page Objects. 我还扩展了硒,以使用相同的等待未映射的页面对象。 You could do something similar with Extending the Driver class as well to match what you are currently doing. 您也可以使用扩展Driver类来做类似的事情,以匹配您当前正在做的事情。

public static void WaitForLocator(this IWebDriver driver, By by, int waitTime)
    {
        Logging.Action("Using locator: " + by + " to find element");
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(waitTime));
        wait.Until(ExpectedConditions.ElementToBeClickable(by)); //There are several Expected conditions
        Logging.Information("Found element with locator: " + by);
    }

This would look like this with your current example 您当前的示例看起来像这样

driver.WaitForLocator(By.XPath('your XPath');

This method will also allow you to use CSS selectors, Ids, etc by changing your By type. 该方法还允许您通过更改By类型来使用CSS选择器,Ids等。

The other way to explicitly wait for elements is using 显式等待元素的另一种方法是使用

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
var saveButton = wait.Until(x => driver.FindElements(By.XPath(Xpath));

Not sure you if meant to use driver.FindElements or driver.FindElement since the name of the method is WaitForElementPresent but you used driver.FindElements. 不确定您是否打算使用driver.FindElements或driver.FindElement,因为方法的名称为WaitForElementPresent,但您使用的是driver.FindElements。 Find elements returns a list of elements not just the element you were looking for. 查找元素将返回元素列表,而不仅仅是您要查找的元素。

Hope this helps. 希望这可以帮助。

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

相关问题 如何在 C# 中使用 Selenium WebDriver 获取当前窗口的 URL? - How to get the URL of the current window using Selenium WebDriver in C#? Selenium WebDriver-如何使用C#设置页面加载超时 - Selenium WebDriver - How to set Page Load Timeout using C# 如何使Selenium Webdriver集中精力(使用C#)? - How do I get Selenium Webdriver to focus (using c#)? 如何让 webDriver 等待页面加载(C# Selenium 项目) - How to get webDriver to wait for page to load (C# Selenium project) 如何使用Selenium Webdriver在Internet Explorer上获取Cookie-C# - How to get cookies on internet explorer using Selenium webdriver - C# 如何使用Selenium Webdriver(C#)获取页面名称? - How to get a page name using Selenium Webdriver (C#)? 页面加载超时 - 使用C#的Selenium Webdriver - Page Load Timeout - Selenium Webdriver using C# 如何通过 Selenium WebDriver 和 C# 在列表 (ul) 中选择项目 (li) - 定位时超时 - How to select an item (li) within a list (ul) through Selenium WebDriver and C# - timeout when locating C#Selenium Webdriver超时回调? 如果超时,如何退出驱动程序? - C# Selenium webdriver timeout callback? How to quit the driver if it times out? Selenium WebDriver C# - 获取文本 - Selenium WebDriver C# - get text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM