Tim*_*imD 1 javascript webautomation node.js puppeteer
我正在尝试使用 Puppeteer 自动执行查询该网站上的数据的任务。因此,我需要选择数据集(每日摘要,第一个选项),然后选择位置类型(州,第三个选项),然后选择州(阿拉斯加,第二个选项)。问题是我的代码没有更改为下一个表。因此,在选择数据集中的第一个选项(每日摘要)后,它不会选择第三个选项(州),而是再次选择数据集表中的第三个选项!我是 Puppeteer 的新手,所以我真的不知道该怎么做。任何帮助表示赞赏。
下面是我的代码:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless:false})
const page = await browser.newPage()
const navigationPromise = page.waitForNavigation()
await page.goto('https://www.ncdc.noaa.gov/cdo-web/datatools/selectlocation')
await page.waitForSelector('.selectLocationFilters > .datasetContainer > .slideElement > #datasetSelect > option:nth-child(1)')
await page.click('.selectLocationFilters > .datasetContainer > .slideElement > #datasetSelect > option:nth-child(1)')
await page.select('.inset #locationCategorySelect', '')
await page.waitForSelector('.selectLocationFilters > .locationCategoryContainer > .locationCategoryFilter > #locationCategorySelect > option:nth-child(3)')
await page.click('.selectLocationFilters > .locationCategoryContainer > .locationCategoryFilter > #locationCategorySelect > option:nth-child(3)')
await page.select('.inset #selectedState', '')
await page.waitForSelector('.selectLocationFilters > .locationContainer > .stateFilter > #selectedState > option:nth-child(2)')
await page.click('.selectLocationFilters > .locationContainer > .stateFilter > #selectedState > option:nth-child(2)')
await browser.close()
})()Run Code Online (Sandbox Code Playgroud)
您遇到的问题是 CSS 转换阻止您单击这些元素。一种可能的解决方案是禁用页面上的所有 CSS 动画。
您可以在调用后添加goto:
await page.addStyleTag({ content : `
*,
*::after,
*::before {
transition-delay: 0s !important;
transition-duration: 0s !important;
animation-delay: -0.0001s !important;
animation-duration: 0s !important;
animation-play-state: paused !important;
caret-color: transparent !important;
}`})
Run Code Online (Sandbox Code Playgroud)