Selenium WebElement.click()与Javascript点击事件 [英] Selenium WebElement.click() vs. Javascript click event

查看:324
本文介绍了Selenium WebElement.click()与Javascript点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道调用 click()方法之间有什么区别/api/javascript/class_webdriver_WebElement.html\"rel =nofollow> WebElement 与按ID查找元素并使用JavaScript触发点击事件。

I was wondering what the differences are between calling the click() method of the WebElement versus finding the element by id and firing the click event with JavaScript.

为了清楚第一种方法,我调用 .click() ://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebElement.html\"rel =nofollow> WebElement

Just to be clear in the first method I call the .click() of an instance of WebElement:

myWebElement.click();

第二种技巧是:

((JavascriptExecutor)driver).executeScript("document.getElementById('myElementID').click()");

我很想知道点击网页元素的这两种技术之间的所有差异,以及优势每个人的缺点。

I'm interested in knowing all the differences between these two techniques for clicking web elements, and also advantages and disadvantages of each.

推荐答案

Webdriver 利用浏览器的原生支持将DOM元素映射到WebElement使用id / xpath等的对象。

Webdriver utilizes a browser's native support for mapping the DOM element to WebElement object using id/xpath etc.

JavascriptExecutor.executeScript 执行外部脚本在当前选定的浏览器窗口的上下文中。 (类似于增强浏览工具,如油脂猴,如果您曾经使用过),
,以防脚本返回任何转换为​​WebElement对象的DOM元素。

The JavascriptExecutor.executeScript executes an external script in the context of the currently selected browser window. (similar to an augmented browsing tool like grease monkey, if you ever used), and in case the script returns any DOM element its converted into WebElement object.

也可以说,WebDriver在浏览器上模拟的点击类似于实际用户与使用javascript调用的点击相比。

One can also say, the click simulated by WebDriver on a browser is similar to what actual user do as compared to one invoked using javascript.

实际上,使用WebDriver并非所有事件都可以通过所有Web浏览器完美自动化,实际上也可以使用同一Web浏览器的不同版本。 (即不同版本的IE,FF等表现不同)。仍然 WebDriver是近乎可用的最佳工具

In reality, with WebDriver not all the events can be automated flawlessly with all the web browsers, in fact with different versions of the same Web browser also. (i.e. different version of IE, FF etc behave differently). Still WebDriver is the near best tool available for this.

在某个版本的IE上我们发现一次(大约4年后)我们发现我们无法发送右键单击或者可能将鼠标悬停在生成的菜单链接上,所以我们使用js来模拟它,它执行了非常多的浏览器独立方式。所以你现在可以得出结论执行外部JavaScript有什么好处。

Once (~4 years back) on a certain version of IE we observed that we can't send right click or may be hover mouse on generated menu links, so we used js to simulate that, which performed very much browser independent way. so you can now conclude what executing external javascript can be good for.

此外,还有自动化的Web测试框架,它们使用javascript代替浏览器的本机支持。例如: http://en.wikipedia.org/wiki/Sahi_%28software%29

Also, there are automated web testing frameworks which use javascript for everything instead of browser's native support. e.g. :http://en.wikipedia.org/wiki/Sahi_%28software%29

参考:

  • http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/JavascriptExecutor.html#executeScript%28java.lang.String,%20java.lang.Object...%29
  • http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#findElement%28org.openqa.selenium.By%29

这篇关于Selenium WebElement.click()与Javascript点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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