【问题标题】:CS1061- does not contain a definition for 'SendKeys' and no accessible extension method 'SendKeys'CS1061-不包含“SendKeys”的定义,也没有可访问的扩展方法“SendKeys”
【发布时间】:2022-03-01 14:42:16
【问题描述】:

我正在尝试为“ElementIsVisible”编写通用代码,以便可以在多个地方使用通用代码。我不断收到此错误。我该如何纠正这个问题?

Error   CS1061  'Shared' does not contain a definition for 'SendKeys' and no accessible extension method 'SendKeys' accepting a first argument of type 'Shared' could be found (are you missing a using directive or an assembly reference?

这是我的代码

 public Shared WaitUntilElementIsVisible(By by)
        {
            WebDriverWait wait = new WebDriverWait(Driver.Instance.webDriver, TimeSpan.FromSeconds(20));
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(by));
            return this;
        }
public Page Physician(string physician)
        {
            
            var WeekStarted = PrShared.Page.WaitUntilElementIsVisible(By.XPath("//input[@id='fyd_weekStarted']"));
            WeekStarted.SendKeys(weekStarted);
            return this;
        }
      
**TO TEST**
Test.Page.Physician(Data.WeekStarted)

【问题讨论】:

    标签: c# selenium selenium-webdriver


    【解决方案1】:

    WaitUntilElementIsVisible() 你有return this 但你还没有定义this 是什么。 WaitUntilElementIsVisible() 中的 wait 返回 IWebElement... 只需返回,但您必须将返回类型更改为 IWebElement

    public IWebElement WaitUntilElementIsVisible(By by)
    {
        WebDriverWait wait = new WebDriverWait(Driver.Instance.webDriver, TimeSpan.FromSeconds(20));
        return wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(by));
    }
    

    另一个建议是为此方法添加一个超时参数,以便您可以调整等待时间,例如

    public IWebElement WaitUntilElementIsVisible(By by, int timeout)
    {
        WebDriverWait wait = new WebDriverWait(Driver.Instance.webDriver, TimeSpan.FromSeconds(timeout));
        return wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(by));
    }
    

    【讨论】:

    • 您还没有显示Shared 是如何定义的,所以我不得不在这里更改返回类型。如果您想返回Shared,则必须更新您的问题并显示您定义Shared 类型的代码。
    猜你喜欢
    • 2020-09-12
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    相关资源
    最近更新 更多