从 executescript 函数访问变量
accessing variables from executescript function
我希望在测试期间使用 browser.executescript 动态设置一些数据。类似于:
var x;
browser.executeScript(function () {
var something = x;
});
但是 x 似乎超出了 运行 的函数范围。有没有办法让我传递将在内部范围内的参数?
非常感谢任何帮助
C
传递里面的参数arguments
:
Any arguments provided in addition to the script will be included as
script arguments and may be referenced using the arguments object.
Arguments may be a boolean, number, string, or webdriver.WebElement.
Arrays and objects may also be used as script arguments as long as
each item adheres to the types previously mentioned.
var x;
browser.executeScript(function (arguments) {
var something = arguments[0];
}, x);
除了@alecxe 的回答之外,如果您想要更易读的代码,并且可以支持 es6,这似乎是一个不错的选择。
更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
var my, params, go, here;
browser.executeScript(function ([my, params, go, here]) {
var something = here;
}, [my, params, go, here]);
我希望在测试期间使用 browser.executescript 动态设置一些数据。类似于:
var x;
browser.executeScript(function () {
var something = x;
});
但是 x 似乎超出了 运行 的函数范围。有没有办法让我传递将在内部范围内的参数?
非常感谢任何帮助 C
传递里面的参数arguments
:
Any arguments provided in addition to the script will be included as script arguments and may be referenced using the arguments object. Arguments may be a boolean, number, string, or webdriver.WebElement. Arrays and objects may also be used as script arguments as long as each item adheres to the types previously mentioned.
var x;
browser.executeScript(function (arguments) {
var something = arguments[0];
}, x);
除了@alecxe 的回答之外,如果您想要更易读的代码,并且可以支持 es6,这似乎是一个不错的选择。 更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
var my, params, go, here;
browser.executeScript(function ([my, params, go, here]) {
var something = here;
}, [my, params, go, here]);