繁体   English   中英

如何使用 Selenium 和 Node.js 从网页中抓取动态呈现的数据?

[英]How to scrape dynamically rendered data from a webpage with Selenium and Node.js?

我目前在抓取一个使用 react 来呈现其网站部分内容的网站时遇到了一些困难,我不确定为什么我不能抓取数据。

这是网站的html:图像

我想要做的是使用data-test="shipItButton"获取按钮的文本

但是,当我运行代码时,出现此错误:

(node:23294) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"[data-test=shipItButton]"}

这是我当前的代码:

const webdriver = require("selenium-webdriver");
const By = webdriver.By;
const until = webdriver.until;

const driver = new webdriver.Builder()
  .forBrowser("chrome")
  .build();

let getData = async(url) => {
  await driver.get(url);
  let element = await driver.findElement(By.css('[data-test=shipItButton]'));
  await driver.wait(until.elementIsVisible(element), 15000)
    .then(element => getText())
    .then(text => console.log("What is text: ", text))
    .catch(err => console.log("Failed to get: ", err));
}
getData("https://www.target.com/p/animal-crossing-new-horizons-8211-nintendo-switch/-/A-76780148")

关于如何解决这个问题的任何想法?

修复这个区域。

By.css("button[data-test='shipItButton']")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM