javascript 当通过 Selenium WebDriver 使用来自 JavascriptExecutor 接口的 executeScript 方法时,arguments[0] 和 arguments[1] 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49871432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:48:15  来源:igfitidea点击:

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

javascriptjavaseleniumselenium-webdriverwebdriver

提问by Vel Guru

What does arguments[0]and arguments[1]mean when using executeScript()method from JavascriptExecutorinterface through Selenium WebDriver and what is the purpose of the arguments[0]in the below code.

是什么arguments[0]以及arguments[1]使用时的意思是executeScript()方法从JavascriptExecutor通过硒webdriver的接口,什么是的目的arguments[0]在下面的代码。

javaScriptExecutor.executeScript("arguments[0].click()", webElement);

回答by DebanjanB

The executeScript()method from the JavascriptExecutorInterface can invoke multiple arguments in the form of arguments[0], arguments[1], etc

来自JavascriptExecutor接口的executeScript()方法可以以arguments[0]arguments[1]等形式调用多个参数

  • As per your example, to javaScriptExecutor.executeScript("arguments[0].click()", webElement);to work you need to have the webElementdefined. executeScript()method will take the reference of the element as arguments[0]along with the methodto 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]
  • 根据您的示例,要javaScriptExecutor.executeScript("arguments[0].click()", webElement);工作,您需要定义webElementexecuteScript()方法将元素的引用作为参数[0]以及要执行的方法[在这种情况下click()],然后应该提供引用。

    WebElement webElement = driver.findElement(By.xpath("xpath_element"));
    JavascriptExecutor javaScriptExecutor = (JavascriptExecutor)driver;
    javaScriptExecutor.executeScript("arguments[0].click()", webElement);
    
  • 同样,executeScript()有多个参数[]的例子如下:

    JavascriptExecutor js = (JavascriptExecutor) driver; 
    js.executeScript("arguments[0].setAttribute('style', arguments[1])", driver.findElement(By.xpath("//input[@type='file']")), "0");
    

    在这个例子中:

    • driver.findElement(By.xpath("//input[@type='file']被称为参数[0]
    • “0”被称为参数[1]

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

您可以在通过 Selenium 和 Python 通过 WebDriver 实例调用 execute_script() 方法时什么是参数 [0] 中找到相关讨论

回答by yong

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

对于 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.

第一个参数是一个 javascript 代码片段或一个 javascript 函数,如果它是一个 javascript 代码片段,它将被包装成一个 javascript 函数内部executeScript

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

接下来的参数是 javascript 函数的参数,表示第一个参数。

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

arguments是 javascript 函数内置功能。您可以使用它在调用函数时获取真正的传入参数。请看下面的例子:

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

有关 Javascript Function.arguments 的更多详细信息

回答by iamsankalp89

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

它似乎在匿名函数的上下文中运行,该函数正在传递任何driver.findElement(locator)生成的内容。

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

那么,arguments[0]您访问匿名函数的第一个参数的方式是什么?arguments[1]