简体   繁体   English

如何从另一个类调用IWebElement方法?

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

I have a program in VS using C# with Selenium that inputs data into a textbox of a certain website. 我有一个在VS中将C#与Selenium结合使用的程序,该程序将数据输入到某个网站的文本框中。 And I found this method online that waits until the element exists. 而且我在线找到了该方法,该方法一直等待到元素存在。

Tried calling the WaitUntilElementExists method from another class but it doesn't work. 试图从另一个类中调用WaitUntilElementExists方法,但是它不起作用。 Don't know if I missed something. 不知道我是否错过了什么。 Appreciate if you could help me out. 谢谢您能帮助我。 Thanks in advance! 提前致谢!

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;
        }
     }
 }

You have two options: 您有两种选择:

Use WaitUntilElementExists as regular static method 使用WaitUntilElementExists作为常规静态方法

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

Or use it as extension method , call it using the IWebDriver instance 或将其用作扩展方法 ,使用IWebDriver实例调用它

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM