Selenium – 带透明代理的MoveToElement()
我有元素
public ArticlePage() { PageFactory.InitElements(Browser.driver, this) } [FindsBy(How = How.Id, Using = "someId")] private IWebElement btnTitleView { get; set; }
和行动
Actions action = new Actions(Browser.driver); action.MoveToElement(btnTitleView).Perform();
但是当我尝试运行它时,我会收到错误
‘System.Reflection.TargetException’对象与目标类型不匹配。
我试图通过Browser.driver.FindElement(By.Id("someId"))
找到这个元素,然后它正常工作。 因此,它存在并显示。
是否可以使用透明代理来执行Actions
? 有没有其他方法可以对透明代理执行MoveToElement()
操作?
为了展开使用透明代理的元素,您可以使用具有WrappedElement
属性的IWrapsElement
接口:
action.MoveToElement(((IWrapsElement)btnTitleView).WrappedElement).Build().Perform();
您可能还希望将该转换包含为IWebElement
对象的扩展方法:
public static class IWebElementExtensions { public static IWebElement Unwrap(this IWebElement element) { return ((IWrapsElement)element).WrappedElement; } }
然后您的操作代码可能如下所示:
Actions action = new Actions(Browser.driver); action.MoveToElement(btnTitleView.Unwrap()).Build().Perform();
我希望答案可以帮助你解决问题:)
解决这个问题的一种方法是使用IList
而不是使用foreach
或LINQ
来操作元素。 所以你可以使用:
[FindsBy(How = How.Id, Using = "someId")] private IList
要么
foreach (var element in btnTitleView) { Actions action = new Actions(Browser.driver); action.MoveToElement(element).Perform(); }