From a technical perspective, how does Selenium click an element on a web page?
The Essentials
The drivers for Chrome, Firefox, and Internet Explorer are all RemoteWebDrivers
.
This means that any actions which Selenium performs are sent to the browser (the WebDriver
), via an HttpRequest
.
Once the request is received by the browser, it will perform the action as either a "native event" or synthetically. How a browser executes an action depends on the capabilities of the browser (and potentially a flag option).
"Native" events are OS-level events.
Actions executed synthetically are executed using JavaScript. "Automation Atoms" are used - as one infers from 'atom', they are small, simple functions to perform low-level actions.
References
RemoteWebDriver
is subclassed byChromeDriver
,FirefoxDriver
,InternetExplorerDriver
,OperaDriver
, andSafariDriver
. (reference)All implementations of WebDriver that communicate with the browser, or a RemoteWebDriver server shall use a common wire protocol. This wire protocol defines a RESTful web service using JSON over HTTP. (reference)
In WebDriver advanced user interactions are provided by either simulating the JavaScript events directly (i.e. synthetic events) or by letting the browser generate the JavaScript events (i.e. native events). Native events simulate the user interactions better whereas synthetic events are platform independent [...] Native events should be used whenever possible. (reference)
Browser Automation Atoms are building blocks intended to be used by Selenium implementations. By using the same pieces throughout the codebase, rather than reimplementing required functionality in multiple places, the project can reduce the number of bugs found, and can simplify the process of adding new functionality and drivers. (reference)
Automation Atoms
- A summary of the available Automation Atoms
- The raw JavaScript code for the Automation Atoms - this may serve as a useful starting point in developing simpler synthetic events, if necessary.
The wiki for the Selenium IE Driver states that it uses native events rather than JavaScript events to interact with the browser.
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.
Except for clicking <option>
elements, where it uses JavaScript.
The IE driver handles this one scenario by using the
click()
Automation Atom, which essentially sets the.selected
property of the element and simulates theonChange
event in JavaScript.