gpt4 book ai didi

java - IE 中的 Selenium Hover 元素

转载 作者:搜寻专家 更新时间:2023-10-31 08:11:00 24 4
gpt4 key购买 nike

我有一个 HTML div 标签,在 div 内部有一个元素,当鼠标进入其边界时会出现。现在我想单击在鼠标进入或悬停时变得可见的元素。

问题:元素开始闪烁。浏览器:IE8

我正在使用下面的代码

   IWebElement we = addToBasket.FindElement(By.Id("MyBox"));
action.MoveToElement(we).MoveToElement(driver.FindElement(By.Id("plus-icon"))).Click().Build().Perform();

它闪烁的原因有什么建议吗?

最佳答案

元素闪烁是因为 IE 驱动程序称为“持续悬停”的功能。此功能的值(value)值得怀疑,但由于 IE(浏览器,而不是驱动程序)的脑死亡方式而需要 responds to WM_MOUSEMOVE messages使用 SendMessage API 时。

您有几个选择。您可以使用如下代码关闭持续悬停:

InternetExplorerOptions options = new InternetExplorerOptions();
options.EnablePersistentHover = false;
IWebDriver driver = new InternetExplorerDriver(options);

请注意,当您尝试悬停时,这会使您随心所欲地确定物理鼠标光标在屏幕上的位置。如果那 Not Acceptable ,你有一个 couple of other approaches你可以拿。首先,您可以关闭所谓的“ native 事件”,这会导致驱动程序仅依赖于合成的 JavaScript 事件。由于仅依赖 JavaScript 来合成鼠标事件,这种方法有其自身的缺陷。

InternetExplorerOptions options = new InternetExplorerOptions();
options.EnableNativeEvents = false;
IWebDriver driver = new InternetExplorerDriver(options);

最后,您可以从使用默认的 SendMessage Windows API 迁移到使用更正确的 SendInput API 的代码。这是通过 RequireWindowFocus 属性完成的。它的缺点是鼠标输入在系统中是在很低的层次上注入(inject)的,这需要IE窗口作为系统的前台窗口。

InternetExplorerOptions options = new InternetExplorerOptions();
options.RequireWindowFocus = true;
IWebDriver driver = new InternetExplorerDriver(options);

最后一点,不要试图一次设置所有这些属性;选择一种方法并坚持下去。其中有几个是互斥的,它们之间的相互作用是不确定的。

关于java - IE 中的 Selenium Hover 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19662045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com