WebDriver点击()vs JavaScript点击() [英] WebDriver click() vs JavaScript click()

查看:181
本文介绍了WebDriver点击()vs JavaScript点击()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

故事:

在StackOverflow上,我看到用户报告他们无法通过selenium点击元素WebDriver点击命令并可以通过执行脚本单击JavaScript来解决它。

Here on StackOverflow, I've seen users reporting that they cannot click an element via selenium WebDriver "click" command and can work around it with a JavaScript click by executing a script.

Python中的示例:

Example in Python:

element = driver.find_element_by_id("myid")
driver.execute_script("arguments[0].click();", element)

WebDriverJS / Protractor中的示例:

Example in WebDriverJS/Protractor:

var elm = $("#myid");
browser.executeScript("arguments[0].click();", elm.getWebElement());

问题:

为什么在常规WebDriver点击时没有点击通过JavaScript?究竟是什么时候发生这种变通方法的缺点(如果有的话)?

Why is clicking "via JavaScript" works when a regular WebDriver click does not? When exactly is this happening and what is the downside of this workaround (if any)?

我个人使用这种解决方法而没有完全理解为什么我必须这样做它会导致什么问题。

推荐答案

目前接受的答案表明,当涉及到WebDriver执行点击并在JavaScript中执行此操作之间的区别时,PhantomJS没有任何特定内容。

Contrarily to what the currently accepted answer suggests, there's nothing specific to PhantomJS when it comes to the difference between having WebDriver do a click and doing it in JavaScript.

这两种方法的本质区别在于所有浏览器都很常见,可以简单地解释一下:

The essential difference between the two methods is common to all browsers and can be explained pretty simply:


  • WebDriver:当WebDriver执行点击时,它尽可能地尝试模拟当真实用户使用浏览器。假设您有一个元素A,它是一个显示Click me的按钮和一个元素B,它是一个 div 元素,它是透明,但有其尺寸和 zIndex 设置,以便它完全覆盖A.然后你告诉WebDriver点击A. WebDriver将模拟点击,以便B收到点击第一。为什么?因为B覆盖A,并且如果用户试图点击A,那么B将首先获得该事件。 A是否最终会获得click事件取决于B如何处理事件。无论如何,在这种情况下,WebDriver的行为与真实用户尝试单击A时的行为相同。

  • WebDriver: When WebDriver does the click, it attempts as best as it can to simulate what happens when a real user uses the browser. Suppose you have an element A which is a button that says "Click me" and an element B which is a div element which is transparent but has its dimensions and zIndex set so that it completely covers A. Then you tell WebDriver to click A. WebDriver will simulate the click so that B receives the click first. Why? Because B covers A, and if a user were to try to click on A, then B would get the event first. Whether or not A would eventually get the click event depends on how B handles the event. At any rate, the behavior with WebDriver in this case is the same as when a real user tries to click on A.

JavaScript:现在,假设您使用要做的JavaScript A.click()此单击方法不会重现用户尝试单击A时发生的实际情况。 JavaScript将单击事件直接发送给A,B将没有得到任何活动。

JavaScript: Now, suppose you use JavaScript to do A.click(). This method of clicking does not reproduce what really happens when the user tries to click A. JavaScript sends the click event directly to A, and B will not get any event.

正如我上面提到的,WebDriver将尽力模拟真实用户使用浏览器时会发生什么。事实上,DOM可以包含用户无法与之交互的元素,而WebDriver将不允许您单击这些元素。除了我提到的重叠案例,这也需要不能点击隐形元素。我在Stack Overflow问题中看到的一个常见情况是尝试与已经存在于DOM中的GUI元素进行交互,但只有在操作了某些其他元素时才会显示。这有时会发生在下拉菜单中:您必须首先单击按钮,然后在选择菜单项之前调出下拉列表。如果有人在菜单可见之前尝试单击菜单项,WebDriver将拒绝并说该元素无法操作。 如果此人尝试使用JavaScript,它会起作用,因为事件会直接传递给元素,而与可见性无关。

As I mentioned above WebDriver will try to simulate as best it can what happens when a real user is using a browser. The fact of the matter is that the DOM can contain elements that a user cannot interact with, and WebDriver won't allow you to click on these element. Besides the overlapping case I mentioned, this also entails that invisible elements cannot be clicked. A common case I see in Stack Overflow questions is someone who is trying to interact with a GUI element that already exists in the DOM but becomes visible only when some other element has been manipulated. This sometimes happens with dropdown menus: you have to first click on the button the brings up the dropdown before a menu item can be selected. If someone tries to click the menu item before the menu is visible, WebDriver will balk and say that the element cannot be manipulated. If the person then tries to do it with JavaScript, it will work because the event is delivered directly to the element, irrespective of visibility.

如果您使用Selenium进行测试应用程序,我对此问题的回答是几乎从来没有。大体上,您的Selenium测试应该重现用户对浏览器的处理方式。以下拉菜单为例:测试应该首先点击下拉按钮,然后单击菜单项。如果GUI出现问题,因为按钮不可见,或者按钮无法显示菜单项或类似内容,那么您的测试将失败并且您将检测到该错误。 如果您使用JavaScript进行点击,则无法通过自动化测试检测到这些错误。

If you are using Selenium for testing an application, my answer to this question is "almost never". By and large, your Selenium test should reproduce what a user would do with the browser. Taking the example of the drop down menu: a test should click on the button that brings up the drop down first, and then click on the menu item. If there is a problem with the GUI because the button is invisible, or the button fails to show the menu items, or something similar, then your test will fail and you'll have detected the bug. If you use JavaScript to click around, you won't be able to detect these bugs through automated testing.

我说几乎从不因为可能存在使用JavaScript有意义的例外情况。但它们应该是非常罕见的。

I say "almost never" because there may be exceptions where it makes sense to use JavaScript. They should be very rare, though.

如果您使用Selenium来抓取网站,那么尝试重现用户并不重要行为。因此,使用JavaScript绕过GUI不是一个问题。

If you are using Selenium for scraping sites, then it is not as critical to attempt to reproduce user behavior. So using JavaScript to bypass the GUI is less of an issue.

这篇关于WebDriver点击()vs JavaScript点击()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆