为什么在node.js运行硒时,我为什么要继续获得nosuchsessionerror?

发布于 2025-01-24 15:43:37 字数 1447 浏览 0 评论 0原文

当我运行脚本时,自动化的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2025-01-31 15:43:37

尝试将以下

let ele = await driver.wait(until.elementLocated(By.id('gbqfbb'), 10000));

更改为

let ele = await driver.wait(until.elementLocated(By.id('gbqfbb')), 10000);

t也很重要的是要注意,如果此方法引发异常(超时例外),因此您必须尝试 -也抓住它。

Try to change the below

From

let ele = await driver.wait(until.elementLocated(By.id('gbqfbb'), 10000));

to

let ele = await driver.wait(until.elementLocated(By.id('gbqfbb')), 10000);

t's also important to notice that if this method throws an exception (timeout exception), so you must try-catch it too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文