Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does arguments[0] and arguments[1] mean when using executeScript method from JavascriptExecutor interface through Selenium WebDriver?

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);
like image 762
Vel Guru Avatar asked Apr 17 '18 06:04

Vel Guru


People also ask

What does the JavaScriptExecutor method executeScript () used for?

JavascriptExecutor consists of two methods that handle all essential interactions using JavaScript in Selenium. executeScript method – This method executes the test script in the context of the currently selected window or frame. The script in the method runs as an anonymous function.

What does the argument 0 represent?

The arguments object has a property named length that returns the number of arguments passed to the function. We can access these parameters with an array-like notation. For example, arguments[0] will return the first argument, arguments[1] will return the second argument, and so on.

What is JavaScriptExecutor and in which case JavaScriptExecutor will help in Selenium automation?

JavaScriptExecutor is an interface that is used to execute JavaScriprt through selenium webdriver. JavaScript is a programming language that interacts with HTML in a browser, and to use this function in Selenium, JavascriptExecutor is required. JavascriptExecutor Provides Two Methods: ExecuteScript.

Which of the following in Selenium is used to type in textbox through JavaScriptExecutor?

We can input text in the text box without the method sendKeys with thehelp of the JavaScript Executor. Selenium executes JavaScript commands with the help of the executeScript method.


3 Answers

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]
    • "0" is referred as arguments[1]

You can find a relevant discussion in What is arguments[0] while invoking execute_script() method through WebDriver instance through Selenium and Python?

like image 82
undetected Selenium Avatar answered Oct 10 '22 17:10

undetected Selenium


For executeScript API: executeScript(script/function, arg1, arg2, arg3, ...)

The first argument is a javascript snippet or a javascript function, if it's a javascript snippet, it will be wrapper into an javascript function inside executeScript.

The next arguments are the arguments for the javascript function represents the first argument.

arguments is javascript function build-in feature. you can use it to get real passed-in arguments when call function. Please see below example:

test('tom', 12, 'male', '175cm') // call function: test

function test(name, age) {
  console.log(name); // tom
  console.log(age);  // 12
  console.log(arguments); // ['tom', 12, 'male', '175cm']
  console.log(arguments[0]); // equal to argument: name, so print tom
  console.log(arguments[1]); // equal to argument: age, so print 12
  console.log(arguments[2]); // male
  console.log(arguments[3]); // 175cm
}

More detail about Javascript Function.arguments

like image 23
yong Avatar answered Oct 10 '22 18:10

yong


It appears to be running in the context of an anonymous function which is getting passed whatever driver.findElement(locator) produces.

So, arguments[0] is your way of accessing the first argument to the anonymous function and same for arguments[1]

like image 1
iamsankalp89 Avatar answered Oct 10 '22 17:10

iamsankalp89