硒获取标题返回空,并且driver.quit()导致异常

时间:2019-04-20 11:27:50

标签: node.js selenium

因此,我最近开始使用Node JS探索硒。我正在测试mozilla website上给出的示例代码。我能够修复其余的内容,但是当这行代码

driver.quit();

导致此异常:

(node:3370) UnhandledPromiseRejectionWarning: NoSuchSessionError: Tried to run command without establishing a connection
at Object.throwDecodedError (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/error.js:550:15)
at parseHttpResponse (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/http.js:542:13)
at Executor.execute (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/http.js:468:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7
(node:3370) 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:3370) [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:3370) UnhandledPromiseRejectionWarning: NoSuchSessionError: Tried to run command without establishing a connection
at Object.throwDecodedError (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/error.js:550:15)
at parseHttpResponse (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/http.js:542:13)
at Executor.execute (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/http.js:468:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7
(node:3370) 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:3370) UnhandledPromiseRejectionWarning: NoSuchSessionError: Tried to run command without establishing a connection
at Object.throwDecodedError (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/error.js:550:15)
at parseHttpResponse (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/http.js:542:13)
at Executor.execute (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/http.js:468:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7
(node:3370) 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:3370) UnhandledPromiseRejectionWarning: NoSuchSessionError: Tried to run command without establishing a connection
at Object.throwDecodedError (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/error.js:550:15)
at parseHttpResponse (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/http.js:542:13)
at Executor.execute (/media/slim/Work/Online_Advertising/Selenium/node_modules/selenium-webdriver/lib/http.js:468:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7
(node:3370) 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: 5)

如果我删除driver.quit(),则不会出现异常。 此外,driver.getTitle()返回空。我的代码如下:

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');
var searchBar = driver.wait(
until.elementLocated(By.name('q'),5000)
);

searchBar.sendKeys('webdriver');
driver.findElement({name:'q'}).sendKeys(webdriver.Key.ENTER);

driver.wait(
until.titleContains("webdriver")
).then(
driver.getTitle().then(function(title) {
console.log(title);
})
);

console.log在终端中输出空白行。 附言我在Linux环境中工作

0 个答案:

没有答案