ChromeOptions使用Selenium ChromeDriver for node.js导致引用错误 [英] ChromeOptions causes reference error using Selenium ChromeDriver for node.js

查看:1046
本文介绍了ChromeOptions使用Selenium ChromeDriver for node.js导致引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Chrome的ChromeDriver驱动程序来运行一些使用Chrome的测试,但是当我使用 ChromeOptions 时出现引用错误。



我的代码



我想强制使用某些选项,例如针对特定用户配置文件进行测试。基于Selenium和ChromeDriver文档,这是我的文件 test.js

  opt = new chromeOptions(); //错误发生在这里! 
opt.setBinary(C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe);
opt.addArguments( - user-data-dir = C:\\ Users \\MyUserAccount\\AppData\\Local\\Google\\Chrome\\ \\\用户数据);
driver = new ChromeDriver(opt);

//我脚本的其余部分转到这里



错误



我使用命令 node test.js 执行此操作。这将在第一行引发以下错误:

  \path\to\test.js:1 

ction(exports,require,module,__filename,__dirname){opt = new chromeOpti
^
ReferenceError:chromeOptions没有在Object中定义
< anonymous> (\path\to\test.js:1:73)
在Module._compile(module.js:456:26)
在Object.Module._extensions..js(module。在Module.load(module.js:356:32)
在Function.Module._load(module.js:312:12)

在Function.Module .runMain(module.js:497:10)
在启动时(node.js:119:16)
在node.js:902:3

值得一提的是,如果我跳过设置选项并用此替换脚本的前四行,它可以工作,但我无法设置选项我需要设置:

  var webdriver = require('selenium-webdriver'); 
var driver = new webdriver.Builder()。
withCapabilities(webdriver.Capabilities.chrome())。
build();

我相信我错过了一些非常基本的东西,但我无法想象这一个出。我如何使用Selenium和node.js设置Chrome的选项?



编辑删除我发现的一些文档中的一些明显无效的语法。

$ p> var webdriver = require(selenium-webdriver);
var chrome = require(selenium-webdriver / chrome);

//确保PATH设置为查找ChromeDriver。我在Unix
//系统上。您需要适应
// Windows所需的任何内容。实际上,既然你说如果你不试图指定选项,你可以让浏览器显示
//,那么你的ChromeDriver就是
//可能已经在你的PATH中,所以你可以跳过这个。
process.env [PATH] + =:/ home / user / src / selenium /;

var options = new chrome.Options();

//注释掉,因为它们显然不是你想要的。
//取消注释并根据需要进行修改:
//
// options.setChromeBinaryPath(/ tmp / foo);
// options.addArguments([ - blah]);

var driver = new webdriver.Builder()。
withCapabilities(options.toCapabilities())。build();

driver.get(http://www.google.com)

我已经用各种值测试了上面的代码,发现它可以工作。



如果你想知道你可以用<$ c $做什么c> Options 对象,可以打开 node_modules / selenium_webdriver / chrome.js 并读取源代码。这是我如何找出上述方法。


I am trying to use the ChromeDriver driver for Selenium to run some tests using Chrome, but I'm getting a reference error when I use ChromeOptions.

My Code

I want to force the use of certain options, such as testing it against a particular user profile. Based on the Selenium and ChromeDriver documentation, this is my file test.js:

opt = new chromeOptions(); // ERROR OCCURS HERE!
opt.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
opt.addArguments("--user-data-dir=C:\\Users\\MyUserAccount\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(opt);

// rest of my script goes here

The error

I am executing this using the command node test.js. This throws the following error on the first line:

\path\to\test.js:1

ction (exports, require, module, __filename, __dirname) { opt = new chromeOpti
                                                                    ^
ReferenceError: chromeOptions is not defined
    at Object.<anonymous> (\path\to\test.js:1:73)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

For what it's worth, if I skip setting options and replace the first four lines of the script with this, it works, but I can't set the options I need to set:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

I'm sure that I'm missing something really basic, but I can't figure this one out. How can I set options for Chrome using Selenium and node.js?

Edited to remove some obviously invalid syntax from the samples in some of the documentation I found.

解决方案

The following works for me:

var webdriver = require("selenium-webdriver");
var chrome = require("selenium-webdriver/chrome");

// Make sure the PATH is set to find ChromeDriver. I'm on a Unix
// system. You'll need to adapt to whatever is needed for
// Windows. Actually, since you say that you can get a browser to show
// up if you don't try to specify options, your ChromeDriver is
// probably already on your PATH, so you can probably skip this.
process.env["PATH"] += ":/home/user/src/selenium/";

var options = new chrome.Options();

// Commented out because they are obviously not what you want.
// Uncomment and adapt as needed:
//
// options.setChromeBinaryPath("/tmp/foo");
// options.addArguments(["--blah"]);

var driver = new webdriver.Builder().
   withCapabilities(options.toCapabilities()).build();

driver.get("http://www.google.com")

I've tested the code above with various values and found that it works.

If you want to see what else you can do with the Options object, you can open node_modules/selenium_webdriver/chrome.js and read the source. This is how I figured out the above method.

这篇关于ChromeOptions使用Selenium ChromeDriver for node.js导致引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆