如何在 Kubuntu 18.04 中为 Firefox 正确设置 Selenium

如何在 Kubuntu 18.04 中为 Firefox 正确设置 Selenium

对于即将开展的自动化项目,我想利用 Firefox 的 Selenium。我遵循本指南。现在,当我想使用“node /path/to/script/scriptName.js”从该指南中运行提到的脚本时,Firefox 窗口会弹出一毫秒并立即关闭。到目前为止,脚本只是从指南中复制/粘贴内容。

Node/npm 和 webdriver 安装成功。geckodriver 位于 '/home' 目录并放入 '$PATH'。

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

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

driver.findElement(By.name('q')).sendKeys('webdriver');

driver.sleep(1000).then(function() {
  driver.findElement(By.name('q')).sendKeys(webdriver.Key.TAB);
});

driver.findElement(By.name('btnK')).click();

driver.sleep(2000).then(function() {
  driver.getTitle().then(function(title) {
    if(title === 'webdriver - Google Search') {
      console.log('Test passed');
    } else {
      console.log('Test failed');
    }
  });
});

driver.quit();

然后控制台抛出了很多错误,但我无法读出这里到底出了什么问题。有人能告诉我,我可能错过了什么吗?

node ~/projects/googleTest.js
(node:21344) UnhandledPromiseRejectionWarning: SessionNotCreatedError: Tried to run command without establishing a connection
    at Object.throwDecodedError (/home/ruphus/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/home/ruphus/node_modules/selenium-webdriver/lib/http.js:542:13)
    at Executor.execute (/home/ruphus/node_modules/selenium-webdriver/lib/http.js:468:26)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:21344) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:21344) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:21344) UnhandledPromiseRejectionWarning: SessionNotCreatedError: Tried to run command without establishing a connection
    at Object.throwDecodedError (/home/ruphus/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/home/ruphus/node_modules/selenium-webdriver/lib/http.js:542:13)
    at Executor.execute (/home/ruphus/node_modules/selenium-webdriver/lib/http.js:468:26)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:21344) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:21344) UnhandledPromiseRejectionWarning: SessionNotCreatedError: Tried to run command without establishing a connection
    at Object.throwDecodedError (/home/ruphus/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/home/ruphus/node_modules/selenium-webdriver/lib/http.js:542:13)
    at Executor.execute (/home/ruphus/node_modules/selenium-webdriver/lib/http.js:468:26)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:21344) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
(node:21344) UnhandledPromiseRejectionWarning: NoSuchSessionError: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.
    at promise.finally (/home/ruphus/node_modules/selenium-webdriver/lib/webdriver.js:726:38)
    at Object.thenFinally [as finally] (/home/ruphus/node_modules/selenium-webdriver/lib/promise.js:124:12)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:21344) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 8)

答案1

我找到了一种解决方法,即安装旧版本的 selenium-webdriver在这种威胁下

npm i [email protected]

相关内容