无法使用WebElement Nodejs

时间:2018-05-17 10:35:23

标签: javascript node.js selenium selenium-webdriver

使用

WebElement img = driver.findElement(By.id("abc"));

我收到以下错误 -

WebElement img = driver.findElement(webdriver.By.id('captchaimg'));
           ^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

我安装的软件包 -

硒的webdriver

chromdriver

的webdriver

版本 -

  • node 8.11.2
  • selenium-webdriver 4.0.0-alpha.1
  • chromedriver 2.38(从here下载)

1 个答案:

答案 0 :(得分:0)

试试这个:

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

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

driver.get('your URL');  
//WebElement img = driver.findElement(webdriver.By.id('abc')); 
// We can't use `WebElement` at here, in javascript, 
// we can't declare the type of variable like Java.
// In your case, JavaScript will treat `WebElement` as variable name,
// Because there already has a variable name, JavaScript don't know 
// how to deal with the following `img`.
// You should change `WebElement` to `var` or `const`

var img = driver.findElement(webdriver.By.id('abc'));