我有一个HTMLdiv
标签,并且在div内有一个元素,当鼠标进入其边界时会出现在其中。现在,我想单击在鼠标进入或悬停时可见的元素。
问题:该元素开始闪烁。浏览器:IE8
我正在使用下面的代码
IWebElement we = addToBasket.FindElement(By.Id("MyBox"));
action.MoveToElement(we).MoveToElement(driver.FindElement(By.Id("plus-icon"))).Click().Build().Perform();
有什么建议为什么它会闪烁吗?
由于IE驱动程序的一个称为“永久悬停”的功能,该元素闪烁。此功能的价值可疑,但是由于IE(浏览器,而不是驱动程序)在使用API时响应WM_MOUSEMOVE
消息的大脑失效方式,因此此功能是必需的SendMessage
。
您有几种选择。您可以使用以下代码关闭永久性悬停:
InternetExplorerOptions options = new InternetExplorerOptions();
options.EnablePersistentHover = false;
IWebDriver driver = new InternetExplorerDriver(options);
请注意,尽管这会使您在尝试悬停时受到物理鼠标光标在屏幕上的位置的影响。如果那是不可接受的,则您可以采用其他两种方法。首先,您可以关闭所谓的“本机事件”,这将导致驱动程序仅依赖于合成的JavaScript事件。由于仅依靠JavaScript来合成鼠标事件,因此这种方法有其自身的陷阱。
InternetExplorerOptions options = new InternetExplorerOptions();
options.EnableNativeEvents = false;
IWebDriver driver = new InternetExplorerDriver(options);
最后,您可以从使用默认的SendMessage
Windows API迁移到使用更正确的SendInput
API的代码。这是通过RequireWindowFocus
属性完成的。它的缺点是鼠标输入是在系统中的非常低的级别上注入的,这要求IE窗口成为系统上的前台窗口。
InternetExplorerOptions options = new InternetExplorerOptions();
options.RequireWindowFocus = true;
IWebDriver driver = new InternetExplorerDriver(options);
最后一点,请不要尝试一次设置所有这些属性。选择一种方法并坚持下去。它们中的几个是互斥的,并且它们之间的交互是不确定的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句