myWebElement.click ();
Actions (driver) .click (myWebElement) .build () performs () ;.
Both the method classes and the click classes belong to the webdriver.Action class, which is used to emulate complex user gestures (including actions such as dragging or dropping multiple items using the Control key, etc.). The click method is used to click on the corresponding web element (buttons, links, etc.) Selenium Webdriver uses the built-in browser support to map the DOM element to the WebElement object using locators such as id / xpath, etc.
JavaScriptExecutor is an interface that provides a mechanism for running Javascript through the selenium driver. It provides the "executescript" and "executeAsyncScript" methods for launching external JavaScript in the context of the currently selected frame or window. In the case of executescript, it returns a DOM element, which is then converted to WebElement
A click simulated by a WebDriver in a browser is similar to what the actual user is doing compared to the one being invoked with javascript
Example script :
<html> <body> <button type = "button" id ="test" style = "display:none"> clickme </button> </body> </html>
If you click on the "click me" button using the click function in webdriver, you will get an org.openqa.selenium.ElementNotVisibleException (an exception not visible to the element), which is correct because the element is present in the DOM but does not appear to the user as a css style display:none set
((JavascriptExecutor)driver).executeScript("$('#test').click();");//or ((JavascriptExecutor)driver).executeScript("document.getElementById('test').click();");
If you use the above javascript / jquery to click an element, it will click on the button regardless of whether the button was visible or not, which is wrong because the end user will not be able to see / click on the button but your script will pass. So, moral is trying to use webdriver functions wherever possible, instead of using javascript
Hope this helps you. Go back if you have any questions.
Vicky
source share