简体   繁体   English

在Node.js上使用Javascript运行Selenium

[英]Running selenium with Javascript on Node.js

I am a noob to Javascript so apologies if my question is very trivial. 我是Java语言的菜鸟,因此如果我的问题很微不足道,我深表歉意。

I am trying to run a selenium test that was wrote in Javascript. 我正在尝试运行用Java语言编写的硒测试。 As I usually do, I just want to start with something simple and work from there. 正如我通常所做的那样,我只想从简单的事情开始,然后从那里开始工作。 In my script I am just trying to load Google using chromedriver. 在我的脚本中,我只是尝试使用chromedriver加载Google。

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

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

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

On the CLI I navigate to the directory where the Test.js file is saved in and I run the command node Test.js . 在CLI上,我导航到保存Test.js文件的目录,然后运行命令node Test.js I always get this error in response; 我总是得到这个错误作为回应。

C:\Selenium\node_modules\selenium-webdriver\_base.js:104
    vm.runInContext(opt_srcText, closure, src);
       ^
SyntaxError: Unexpected token )
at goog.loadModuleFromSource_ (C:\Selenium\node_modules\selenium-webdriver\l
at Object.goog.loadModule (C:\Selenium\node_modules\selenium-webdriver\lib\g
at C:\Selenium\node_modules\selenium-webdriver\lib\webdriver\promise.js:1:6
at Object.Context.closure.goog.retrieveAndExecModule_ (C:\Selenium\node_modu
at <anonymous>:1:6
at Context.closure.closure.vm.createContext.CLOSURE_IMPORT_SCRIPT (C:\Seleni
at Object.goog.importScript_ (C:\Selenium\node_modules\selenium-webdriver\li
at Object.goog.importModule_ (C:\Selenium\node_modules\selenium-webdriver\li
at Object.goog.writeScripts_ (C:\Selenium\node_modules\selenium-webdriver\li
at Object.goog.require (C:\Selenium\node_modules\selenium-webdriver\lib\goog

I originally ran this code on my Windows machine and when I was getting that error I put it down to Windows and Node.js no agreeing and tried it on my Mac. 我最初是在Windows计算机上运行此代码的,当遇到该错误时,我将其放到Windows和Node.js上并没有同意,然后在Mac上进行了尝试。 Still no luck as I was getting the exact same response. 仍然运气不佳,因为我得到了完全相同的答复。

On both machines I have node and npm installed. 在两台机器上,我都安装了node和npm。 Previous to executing the tests I ran the command npm install selenium-webdriver and I also added chromedriver to my PATH. 在执行测试之前,我运行了npm install selenium-webdriver命令,并且还将chromedriver添加到了PATH中。

I have no idea what I am doing wrong so if anyone can point me in the right direction, it'd be very much appreciated. 我不知道我在做什么错,所以如果有人能指出我正确的方向,将不胜感激。

Turns out the version of node I was using was too old. 原来我使用的节点版本太旧。 Thanks to @Louis for your help in reaching this solution. 感谢@Louis为您提供此解决方案的帮助。

What I did was uninstall node and re-installed it with the latest version. 我所做的是卸载节点,然后重新安装了最新版本。 I would imagine upgrading would work too. 我想升级也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM