簡體   English   中英

如何從另一個類調用IWebElement方法?

[英]How to Call IWebElement method from another class?

我有一個在VS中將C#與Selenium結合使用的程序,該程序將數據輸入到某個網站的文本框中。 而且我在線找到了該方法,該方法一直等待到元素存在。

試圖從另一個類中調用WaitUntilElementExists方法,但是它不起作用。 不知道我是否錯過了什么。 謝謝您能幫助我。 提前致謝!

public static void InputTextbox(IWebDriver wDriver, string sElement, string sValue, int iIndex)
    {
        //calling WaitUntilElementExists method
        var wait = new WebDriverWait(wDriver, TimeSpan.FromSeconds(20));
        wait.Until(ExpectedConditions.ElementExists(By.Name("value")));

        //input text box
    }



public static class WaitForElement
{
    public static IWebElement WaitUntilElementExists(this IWebDriver wDriver, By elementLocator, int iTimeout)
    {
        try
        {
            if (iTimeout > 0)
            {
                var wait = new WebDriverWait(wDriver, TimeSpan.FromSeconds(iTimeout));
                return wait.Until(ExpectedConditions.ElementExists(elementLocator));
            }
            return wDriver.FindElement(elementLocator);
        }
        catch (NoSuchElementException)
        {
            Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
            throw;
        }
     }
 }

您有兩種選擇:

使用WaitUntilElementExists作為常規靜態方法

IWebElement element = WaitForElement.WaitUntilElementExists(driver, By.Name("value"), 20);

或將其用作擴展方法 ,使用IWebDriver實例調用它

IWebElement element = driver.WaitUntilElementExists(By.Name("value"), 20);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM