最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Selenium WebElement.click() vs. Javascript click event - Stack Overflow

programmeradmin1浏览0评论

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.

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

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.

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.

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

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.

Share Improve this question edited Mar 6, 2016 at 9:38 demouser123 4,26412 gold badges52 silver badges86 bronze badges asked Jul 4, 2014 at 9:29 cloudy_weathercloudy_weather 2,97713 gold badges42 silver badges64 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

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

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.

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.

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.

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.

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

Ref:

  • 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

Those kind of tests are E2E (end to end) not 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 then callback to run next action.

发布评论

评论列表(0)

  1. 暂无评论