【发布时间】:2021-03-29 21:29:04
【问题描述】:
我正在运行一个网络抓取脚本,我找不到任何可靠的方法来允许在脚本中运行下一行代码之前单击链接时重新加载页面。以下是我目前正在做的事情,虽然它有效,但我知道它取决于加载时间并且它绝对不是有效的。有谁知道我可以使用的 doThisWhenPageLoads() 方法是硒。顺便说一句,我正在使用节点,以防万一。
setTimeout(async () => {
await driver.findElement(By.xpath('//*[@id="navigation"]/div[1]/ul/li[1]/div/div[1]/a')).click()
}, 1500)
// setTimeout(async () => {
await driver.manage().timeouts().implicitlyWait(10, 10).findElement(By.xpath('//*[@id="navigation"]/div[1]/ul/li[1]/div/div[1]/div/div[2]/div[1]/ul/li[2]/a')).click()
// }, 2500)
setTimeout(async () => {
await driver.findElement(By.xpath('//*[@id="content"]/div/div/div[2]/div/header/div/div[3]/nav/ul/li[1]/div/div/ul/li[6]/a')).click()
}, 4500)
setTimeout(async () => {
await driver.findElement(By.xpath('//*[@id="content"]/div/div/div[2]/div/header/div/div[3]/nav/ul/li[1]/div/div[2]/ul/li[6]/ul/li[3]/a')).click()
}, 6500)
setTimeout(async () => {
await driver.findElement(By.xpath('//*[@id="content"]/div/div/div[2]/div/header/div/div[3]/nav/ul/li[1]/div/div[2]/ul/li[6]/ul/li[3]/ul/li[2]/a')).click()
}, 8500)
setTimeout(async () => {
const target = await driver.findElements(By.xpath('//ul[contains(@class, "product-list")]/li'))
let arr = await target.map(async (x, index) => {
let title = await x.findElement(By.xpath(`//li[${index + 1}]/div/div/div/div/div/div`)).getText()
let price = await x.findElement(By.xpath(`//li[${index + 1}]/div/div/div/div/div[2]/form/div/div/div`)).getText()
let pricePer100ml = await x.findElement(By.xpath(`//li[${index + 1}]/div/div/div/div/div[2]/form/div/div/div[2]`)).getText()
let ok = { title, price, pricePer100ml }
console.log(ok)
return ok
})
console.log(arr)
}, 13500)
【问题讨论】:
-
如果是标准页面加载,默认情况下 Selenium 已经等待(对于 PageReady 状态)。如果点击生成的是 javascript DOM 更新,则需要使用 webdriverwait:selenium.dev/selenium/docs/api/dotnet/html/…
标签: node.js selenium selenium-webdriver web-scraping