In case, these locators do not work you can use JavaScriptExecutor. You can use JavaScriptExecutor to perform the desired operation on a web element.
Selenium supports javascript executor. There is no need for an extra plugin or add-on. You just need to import (org.openqa.selenium.JavascriptExecutor) in the script to use JavaScriptExecutor.
JavaScriptExecutor Methods
- executeAsyncScript
With Asynchronous script, your page renders more quickly. Instead of forcing users to wait for a script to download before the page renders. This function will execute an asynchronous piece of JavaScript in the context of the currently selected frame or window in Selenium. The JS so executed is single-threaded with a various callback function which runs synchronously.
- executeScript
This method executes JavaScript in the context of the currently selected frame or window in Selenium. The script used in this method runs in the body of an anonymous function (a function without a name). We can also pass complicated arguments to it.
The script can return values. Data types returned are
- Boolean
- Long
- String
- List
- WebElement.
The basic syntax for JavascriptExecutor is given below:
Syntax:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);
- Script – This is the JavaScript that needs to execute.
- Arguments – It is the arguments to the script. It's optional.