What does arguments[0]
and arguments[1]
mean when using executeScript()
method from JavascriptExecutor interface through Selenium WebDriver and what is the purpose of the arguments[0]
in the below code.
javaScriptExecutor.executeScript("arguments[0].click()", webElement);
The executeScript() method from the JavascriptExecutor Interface can invoke multiple arguments in the form of arguments[0], arguments[1], etc
As per your example, to javaScriptExecutor.executeScript("arguments[0].click()", webElement);
to work you need to have the webElement defined. executeScript()
method will take the reference of the element as arguments[0] along with the method to be performed [in this case click()
] and the reference should be provided thereafter.
WebElement webElement = driver.findElement(By.xpath("xpath_element"));
JavascriptExecutor javaScriptExecutor = (JavascriptExecutor)driver;
javaScriptExecutor.executeScript("arguments[0].click()", webElement);
Similarly, an example of executeScript()
with multiple arguments[] is as follows :
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1])", driver.findElement(By.xpath("//input[@type='file']")), "0");
Here in this example :
driver.findElement(By.xpath("//input[@type='file']
is referred as arguments[0]You can find a relevant discussion in What is arguments[0] while invoking execute_script() method through WebDriver instance through Selenium and Python?