在 selenium 中单击相当于双击

Single click in selenium acts as double click

我有一个简单的代码,我点击 link 并打开一个新的 window。但是在执行脚本时,单击相当于双击同一元素,并且打开了 2 windows。

我正在使用 InternetExplorer 驱动程序

String baseURL = "URL_to_opened";

DesiredCapabilities cap = DesiredCapabilities.internetExplorer();

cap.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);

cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

 WebDriver driver = new InternetExplorerDriver(cap);

driver.get(baseURL);

driver.findElement(By.xpath("Element to be clicked")).click();

当您使用 Selenium 3.4.0IEDriverServer 3.4.0IE(v 10/11),可以考虑通过DesiredCapabilities Class:

传递以下配置属性

Native Events: As the InternetExplorerDriver is Windows-only, it attempts to use so-called "native", or OS-level events to perform mouse and keyboard operations in the browser. This is in contrast to using simulated JavaScript events for the same operations. The advantage of using native events is that it does not rely on the JavaScript sandbox, and it ensures proper JavaScript event propagation within the browser. However, there are currently some issues with mouse events when the IE browser window does not have focus, and when attempting to hover over elements.

Browser Focus:The challenge is that IE itself appears to not fully respect the Windows messages we send the IE browser window (WM_MOUSEDOWN and WM_MOUSEUP) if the window doesn't have the focus. Specifically, the element being clicked on will receive a focus window around it, but the click will not be processed by the element. Arguably, we shouldn't be sending messages at all; rather, we should be using the SendInput() API, but that API explicitly requires the window to have the focus.

You can find more documentation about these facts in this link.

示例代码块:

DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability(InternetExplorerDriver.NATIVE_EVENTS, true);
cap.setCapability(InternetExplorerDriver.REQUIREWINDOWFOCUS, true);
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new InternetExplorerDriver(cap);