我正在尝试使用 Scrapy-Selenium 运行此代码。每当我尝试执行此操作时,selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
都会发生错误。我不知道为什么会这样!除此之外,我想从“product_tab”列表中抓取所有页面(几乎 447 个产品),但它只抓取一个页面。
import scrapy
from scrapy_selenium import SeleniumRequest
import time
from daraz.settings import *
from scrapy.selector import Selector
class ProductsSpider(scrapy.Spider):
name = 'products'
allowed_domains = ['www.daraz.com.bd']
start_urls = ['https://www.daraz.com.bd/wow/i/bd/landingpage/flash-sale?spm=a2a0e.home.flashSale.1.735212f797l5Fo&wh_weex=true&wx_navbar_transparent=true&scm=1003.4.icms-zebra-100031732-2896540.OTHER_5530854870_2643759&skuIds=164180152,184533214,129330070,132110291,169152164,131854718,189322173']
def parse(self, response):
url = "https://www.daraz.com.bd/wow/i/bd/landingpage/flash-sale?spm=a2a0e.home.flashSale.1.735212f797l5Fo&wh_weex=true&wx_navbar_transparent=true&scm=1003.4.icms-zebra-100031732-2896540.OTHER_5530854870_2643759&skuIds=164180152,184533214,129330070,132110291,169152164,131854718,189322173"
yield SeleniumRequest(url=url, callback=self.parse_result)
def parse_result(self, response):
driver = response.request.meta['driver']
product_tab = driver.find_elements_by_xpath("//div[@class='item-list-content']/a")
for product in product_tab:
link = product.find_element_by_xpath("./div/div[2]")
link.click()
time.sleep(1)
html = driver.page_source
resp = Selector(text=html)
yield {
'Title': resp.xpath(".//span[@class='pdp-mod-product-badge-title']/text()").get()
}
输出
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document