Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Selenium C在POM中正确等待#_C#_Selenium - Fatal编程技术网

C# 如何使用Selenium C在POM中正确等待#

C# 如何使用Selenium C在POM中正确等待#,c#,selenium,C#,Selenium,我在测试中设置了一些显式等待,但必须为XPath、ID等指定字符串。因此,我的页面设置包括以下内容 public const string ApplyDatasetButton_XPath = "//*[@id='btn_apply_datasets']"; [FindsBy(How = How.XPath, Using = ApplyDatasetButton_XPath)] public IWebElement ApplyDatasetButton { get; set; } …然后我在我

我在测试中设置了一些显式等待,但必须为XPath、ID等指定字符串。因此,我的页面设置包括以下内容

public const string ApplyDatasetButton_XPath = "//*[@id='btn_apply_datasets']";
[FindsBy(How = How.XPath, Using = ApplyDatasetButton_XPath)]
public IWebElement ApplyDatasetButton { get; set; }
…然后我在我的测试中使用这个,就像

SeleniumGetMethods.WaitForElementVisible(By.XPath(ApplyDatasetButton_XPath), 20);
…其中等待设置为

public static void WaitForElementVisible(By element, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            WebDriverWait wait = new WebDriverWait(DriverSetup.driver, TimeSpan.FromSeconds(timeoutInSeconds));
            wait.Until(ExpectedConditions.ElementIsVisible(element));
        }
    }
然而,创建字符串似乎有些过分,但我不知道如何使用创建的元素。基本上,在C#中有没有一种方法可以做到像

[FindsBy(How = How.XPath, Using = "//*[@id='btn_apply_datasets']")]
public IWebElement ApplyDatasetButton { get; set; }

SeleniumGetMethods.WaitForElementVisible(ApplyDatasetButton, 20);

适用于以下预期条件的延伸件:

/// <summary>
    /// An expectation for checking that an element is present on the DOM of a page
    /// and visible. Visibility means that the element is not only displayed but
    /// also has a height and width that is greater than 0.
    /// </summary>
    /// <param name="locator">The locator used to find the element.</param>
    /// <returns>The <see cref="T:OpenQA.Selenium.IWebElement" /> once it is located and visible.</returns>
    public static Func<IWebDriver, IWebElement> ElementIsVisible(IWebElement element) => driver =>
    {
        try
        {
            return ElementIfVisible(element);
        }
        catch (StaleElementReferenceException)
        {
            return null;
        }
    };

    private static IWebElement ElementIfVisible(IWebElement element)
    {
        if (!element.Displayed)
            return null;
        return element;
    }
用法:

[FindsBy(How = How.XPath, Using = "//*[@id='btn_apply_datasets']")]
public IWebElement ApplyDatasetButton { get; set; }
IWebDriver driver = new ChromeDriver();
driver.WaitUntilElementIsVisible(ApplyDatasetButton, 20);

适用于以下预期条件的延伸件:

/// <summary>
    /// An expectation for checking that an element is present on the DOM of a page
    /// and visible. Visibility means that the element is not only displayed but
    /// also has a height and width that is greater than 0.
    /// </summary>
    /// <param name="locator">The locator used to find the element.</param>
    /// <returns>The <see cref="T:OpenQA.Selenium.IWebElement" /> once it is located and visible.</returns>
    public static Func<IWebDriver, IWebElement> ElementIsVisible(IWebElement element) => driver =>
    {
        try
        {
            return ElementIfVisible(element);
        }
        catch (StaleElementReferenceException)
        {
            return null;
        }
    };

    private static IWebElement ElementIfVisible(IWebElement element)
    {
        if (!element.Displayed)
            return null;
        return element;
    }
用法:

[FindsBy(How = How.XPath, Using = "//*[@id='btn_apply_datasets']")]
public IWebElement ApplyDatasetButton { get; set; }
IWebDriver driver = new ChromeDriver();
driver.WaitUntilElementIsVisible(ApplyDatasetButton, 20);