为什么在node.js运行硒时,我为什么要继续获得nosuchsessionerror?
当我运行脚本时,自动化的Web浏览器会加载,我希望它加载的网站也是如此,但它立即关闭而无需做任何其他操作。我看过这些文档并使用了等待方法,以防我想加载的网站可以加载很多,但似乎仍然无法正常工作。谁能解释一下发生了什么以及我如何遇到此错误?
这是JavaScript代码:
const {Builder, By, until} = require('selenium-webdriver');
(async function helloSelenium() {
let driver = await new Builder().forBrowser('safari').build();
await driver.get('https://www.google.com/?client=safari');
let ele = await driver.wait(until.elementLocated(By.id('gbqfbb'), 10000));
let b = await driver.findElement(By.id('gbqfbb'));
console.log(b.getText());
await driver.quit();
})();
这是错误:
user@MacBook-Pro webscraper % node webscraper.js
Promise { <pending> }
/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/error.js:522
let err = new ctor(data.message)
^
NoSuchSessionError
at Object.throwDecodedError (/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/error.js:522:15)
at parseHttpResponse (/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/http.js:548:13)
at Executor.execute (/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/http.js:474:28)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Driver.execute (/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/webdriver.js:735:17) {
remoteStacktrace: ''
}
When I run the script, the automated web browser does load and so does the website I wanted it to load but it shuts off instantly without doing anything else. I had looked at the docs and used the wait method in case the website I was trying to load does a lot of AJAX loading but it still doesn't seem to be working. Can anyone please explain what is going on and how I am getting this error?
Here is the JavaScript code:
const {Builder, By, until} = require('selenium-webdriver');
(async function helloSelenium() {
let driver = await new Builder().forBrowser('safari').build();
await driver.get('https://www.google.com/?client=safari');
let ele = await driver.wait(until.elementLocated(By.id('gbqfbb'), 10000));
let b = await driver.findElement(By.id('gbqfbb'));
console.log(b.getText());
await driver.quit();
})();
And here is the error:
user@MacBook-Pro webscraper % node webscraper.js
Promise { <pending> }
/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/error.js:522
let err = new ctor(data.message)
^
NoSuchSessionError
at Object.throwDecodedError (/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/error.js:522:15)
at parseHttpResponse (/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/http.js:548:13)
at Executor.execute (/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/http.js:474:28)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Driver.execute (/Users/user/Documents/Web/webscraper/node_modules/selenium-webdriver/lib/webdriver.js:735:17) {
remoteStacktrace: ''
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将以下
从
更改为
t也很重要的是要注意,如果此方法引发异常(超时例外),因此您必须尝试 -也抓住它。
Try to change the below
From
to
t's also important to notice that if this method throws an exception (timeout exception), so you must try-catch it too.