为什么我不能在初始化时使用 Selenium WebDriver for C# 和 PageFactory 执行鼠标移动 [英] Why I can't perform mouse move using Selenium WebDriver for C# and PageFactory in initialization
问题描述
我有一个 HTML 链接,我使用页面对象模式用 Selenium 编写脚本.但是当使用 pageFactory 初始化对象时,我无法执行 MouseMove 操作.所以,我有这样的课:
I have an link in HTML and I use Page Object pattern to write scripts with Selenium. But I can't perform MouseMove action when object is initialized with pageFactory. So, I have such class:
class BingPage
{
private readonly IWebDriver driver;
public static readonly String BASE_URL = "http://bing.com/";
[FindsBy(How = How.XPath, Using = ".//*[@id='scpt2']/a[text()='Shopping']")]
private IWebElement ShoopingLink;
public BingPage(IWebDriver driver)
{
this.driver = driver;
//Page Factory will use Driver to init searchButton and queryEdit objects
PageFactory.InitElements(driver, this);
}
public void HoverShoppingLink()
{
Actions builder = new Actions(driver);
IWebElement elem = driver.FindElement(By.XPath(".//*[@id='scpt2']/a[text()='Shopping']"));
builder.MoveToElement(elem).Build().Perform();//This will work
builder.MoveToElement(ShoopingLink).Build().Perform(); //This will fail
}
}
标有注释这将失败"的行将抛出异常必须为移动操作提供位置.参数名称:actionTarget"
Line marked with comment "This will fail" will throw an exception "Must provide a location for a move action. Parameter name: actionTarget"
但是当我手动查找元素时 - 它可以工作.有人能告诉我为什么吗?
But when I manually lookup element - it works. Could someone tell me why?
.Net 为 selenium 2.0,.Net 4.0 为 IE 驱动程序.
It is selenium 2.0 for .Net, .Net 4.0 with IE driver.
推荐答案
因为为元素创建的代理对象 (ProxiedWebElementInterceptor
) 不代理 ILocatable
,即Actions
类对其方法的要求.这是 .NET 绑定中的一个错误.它已在当前源中修复,并将在下一个公开版本 (2.21) 中提供,即将推出.
Because the proxy object (ProxiedWebElementInterceptor
) created for the element doesn't proxy ILocatable
, which is what the Actions
class requires of its methods. This is a bug in the .NET bindings. It has been fixed in the current sources, and will be available in the next public release (2.21), available soon.
这篇关于为什么我不能在初始化时使用 Selenium WebDriver for C# 和 PageFactory 执行鼠标移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!