在 node javascript 中使用 selenium-webdriver 执行“命令”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19103635/
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-27 14:22:08  来源:igfitidea点击:

executing "commands" using selenium-webdriver in node javascript

javascriptnode.jsseleniumwebdriver

提问by Alex C

I'm interested in executing a number of the advanced "Commands" via the javascript API https://code.google.com/p/selenium/source/browse/javascript/webdriver/command.js

我有兴趣通过 javascript API https://code.google.com/p/selenium/source/browse/javascript/webdriver/command.js执行一些高级“命令”

If I start with the base code:

如果我从基本代码开始:

var browser = new webdriver
        .Builder()
        .usingServer(server.address())
        .withCapabilities(webdriver.Capabilities.phantomjs())
        .build();

every form of "likely" syntax I've tried to execute has failed. for example:

我尝试执行的每种形式的“可能”语法都失败了。例如:

// does not work
console.log(webdriver.Command('getWindowSize'))
// does not work
console.log(browser.Command('getWindowSize'))

Does anyone know how to execute "get window size", or "set window size" in selenium javascript webdriver?

有谁知道如何在 selenium javascript webdriver 中执行“获取窗口大小”或“设置窗口大小”?

回答by Douglas Treadwell

You are probably looking for driver.executeScript.

您可能正在寻找driver.executeScript.

Example:

例子:

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.executeScript('return 2').then(function(return_value) {
    console.log('returned ', return_value)
});

This will log 2 to the console.

这会将 2 记录到控制台。

I also tested this with:

我还测试了这个:

driver.get('http://underscorejs.org/');

driver.executeScript('return _').then(function(return_value) {
    console.log('returned ', return_value)
});

... which correctly lists all the methods defined on _, so it appears to work.

...它正确地列出了 _ 上定义的所有方法,因此它似乎可以工作。

回答by AdamW

In my npm package there's a file node_modules/selenium-webdriver/test/execute_script_test.jswith a great many examples using driver.executeScript(at the very end of the file they define executeas an alias for executeScript).

在我的 npm 包中,有一个node_modules/selenium-webdriver/test/execute_script_test.js包含大量使用示例的文件driver.executeScript(在文件的最后,它们定义execute为 的别名executeScript)。

回答by Aditya

You need driver.executeScript(). You can either pass a function or a string.

你需要 driver.executeScript()。您可以传递函数或字符串。

const _scraper = () => {
  return document.title;
}

const getPageTitle = async (url) => {
  await driver.get(url);
  const pageTitle = await driver.executeScript(_scraper);
  console.log(pageTitle);
}

You can also pass argument to the function.

您还可以将参数传递给函数。

const _scraper1 = (arg) => {
  return document.getElementsByTagName(arg);
}

const getTagData = async (url) => {
  await driver.get(url);
  const metas = await driver.executeScript(_scraper, 'meta');
  console.log(metas);
}