CS1061报错:Shared类无SendKeys定义,Selenium通用等待方法如何修正
报错原因
你封装的WaitUntilElementIsVisible方法返回值为Shared类型,而SendKeys是Selenium中IWebElement接口的原生方法,Shared类既没有定义该方法,也没有对应的扩展方法,因此触发CS1061编译错误。
修正方案
方案1:调整等待方法返回值为可见元素(最推荐)
直接修改等待方法,等待完成后返回定位到的元素实例,后续可直接调用Selenium元素的原生方法:
public IWebElement WaitUntilElementIsVisible(By by) { WebDriverWait wait = new WebDriverWait(Driver.Instance.webDriver, TimeSpan.FromSeconds(20)); // 等待完成后直接返回匹配到的可见元素 return wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(by)); }
调用代码仅需要适配返回值即可:
public Page Physician(string weekStarted) { // 此处拿到的是IWebElement实例,可直接调用SendKeys var WeekStarted = PrShared.Page.WaitUntilElementIsVisible(By.XPath("//input[@id='fyd_weekStarted']")); WeekStarted.SendKeys(weekStarted); return this; }
方案2:保留链式调用风格,在Shared类封装操作方法
如果你希望保留返回Shared实例的链式调用习惯,可以在Shared类中封装SendKeys等常用元素操作方法:
public class Shared { // 原有等待方法保持不变 public Shared WaitUntilElementIsVisible(By by) { WebDriverWait wait = new WebDriverWait(Driver.Instance.webDriver, TimeSpan.FromSeconds(20)); wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(by)); return this; } // 新增封装的SendKeys方法 public Shared SendKeys(By by, string content) { Driver.Instance.webDriver.FindElement(by).SendKeys(content); return this; } }
调用时使用链式写法即可:
public Page Physician(string weekStarted) { By weekStartedInput = By.XPath("//input[@id='fyd_weekStarted']"); PrShared.Page.WaitUntilElementIsVisible(weekStartedInput) .SendKeys(weekStartedInput, weekStarted); return this; }
额外提示:你原有
Physician方法的入参名是physician,但方法内使用的变量是weekStarted,属于笔误,修改时注意同步修正参数名避免后续报错。
内容的提问来源于stack exchange,提问作者USER_124
相关产品推荐
相关产品推荐

