Selenium WebElement.click() 与 Javascript 点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24571048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium WebElement.click() vs. Javascript click event
提问by cloudy_weather
I was wondering what the differences are between calling the click()
method of the WebElementversus finding the element by id and firing the click
event with JavaScript.
我想知道调用WebElement的click()
方法与通过 id 查找元素并使用 JavaScript触发事件之间有什么区别。click
Just to be clear in the first method I call the .click()
of an instance of WebElement:
为了清楚起见,我在第一种方法中调用了WebElement.click()
的实例:
myWebElement.click();
The second technique is:
第二种技术是:
((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.
我很想知道这两种单击 Web 元素的技术之间的所有差异,以及每种技术的优缺点。
采纳答案by Shail016
Webdriverutilizes a browser's native support for mapping the DOM element to WebElement object using id/xpath etc.
Webdriver利用浏览器的本机支持,使用 id/xpath 等将 DOM 元素映射到 WebElement 对象。
The JavascriptExecutor.executeScript
executes an externalscript 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.
该JavascriptExecutor.executeScript
执行的外部在当前选择的浏览器窗口的上下文脚本。(类似于一个增强的浏览工具,如油脂猴子,如果您曾经使用过的话),并且如果脚本返回任何 DOM 元素,则将其转换为 WebElement 对象。
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.
也可以说,与使用 javascript 调用的点击相比,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 toolavailable for this.
实际上,使用 WebDriver 并非所有事件都可以在所有 Web 浏览器中完美地自动化,事实上,对于同一 Web 浏览器的不同版本也是如此。(即不同版本的 IE、FF 等表现不同)。仍然WebDriver 是可用于此的近乎最佳的工具。
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.
曾经(大约 4 年前)在某个版本的 IE 上,我们观察到我们无法在生成的菜单链接上发送右键单击或鼠标悬停,因此我们使用 js 来模拟它,它以非常独立于浏览器的方式执行。因此,您现在可以得出执行外部 javascript 的好处。
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
此外,还有一些自动化的 Web 测试框架,它使用 javascript 来代替浏览器的本机支持。例如:http: //en.wikipedia.org/wiki/Sahi_%28software%29
Ref:
参考:
回答by Krzysztof Safjanowski
Those kind of tests are E2E(end to end) not BDD.
那些类型的测试是E2E(端到端)而不是BDD。
First one – is executed now, to take next action you must write some function that will delay execution for e.g download new data from server.
第一个 - 现在执行,要采取下一步行动,您必须编写一些延迟执行的函数,例如从服务器下载新数据。
The second code return promise
– http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebElement.html– ?Schedules a command to click on this element.” – you can use thencallback to run next action.
第二个代码返回promise
- http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebElement.html- ?调度命令以单击此元素。” – 您可以使用then回调来运行下一个操作。