试图用 selenium 刮下一页

x33g5p2x  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(113)

你好,所以我正在尝试刮谷歌购物网站的下一页。但每当我的刮刀点击下一步按钮时,我都会收到错误消息。它在加载下一页时停止工作。
这是我的码块
从Selify导入WebDriver
进口时间
来自selenium.webdriver.chrome.service导入服务
从selenium.webdriver.Common.by导入
作为PD进口Pandas
Url=‘https://www.google.com.ng/search?q=list+of+all+uk+e-commerce+stores+for+buying+prada+products&hl=en&biw=946&bih=625&tbm=lcl&sxsrf=ALiCzsaIKyYpvCJVWZx_fYTwSQerSvzC6g%3A1667482905673&ei=GcVjY4fUKJeG9u8PgvGwoAE&ved=0ahUKEwjHxIvykZL7AhUXg_0HHYI4DBQQ4dUDCAk&uact=5&oq=list+of+all+uk+e-commerce+stores+for+buying+prada+products&gs_lp=Eg1nd3Mtd2l6LWxvY2FsuAED-AEBMgUQABiiBDIHEAAYHhiiBDIFEAAYogQyBRAAGKIEwgIEECMYJ0iSHFDlBliOFHAAeADIAQCQAQCYAYYDoAHxDqoBBTItMS41iAYB&sclient=gws-wiz-local#rlfi=hd:;si:;mv:[[56.121909699999996,0.16756959999999999],[51.208233299999996,-4.5053765]]’
服务=Service(executable_path=“C:/driver/chromedriver_win32/chromedriver.exe”)
驱动程序=webdriver.Chrome(服务=服务)
Driver.get(Url)
Driver.Maximum_Window()
睡眠时间(8)
对于范围(7)内的i:

site_cards = driver.find_elements(By.CLASS_NAME, 'uMdZh')
time.sleep(4)

site_list = []

for card in site_cards:
    name = card.find_element(By.CLASS_NAME, 'OSrXXb').text.strip()
    #time.sleep(8)
    submit = card.find_element(By.CLASS_NAME, 'OSrXXb')
    submit.click()
    time.sleep(4)
    try:
        more = driver.find_element(By.CLASS_NAME, 'Yy0acb').text.strip()
    except:
        print('none')
    try:
        more = driver.find_element(By.CLASS_NAME, 'mPcsfb').text.strip()
    except:
        print('none')
    time.sleep(2)
    try:
        more = driver.find_element(By.CLASS_NAME, 'YhemCb').text.strip()
    except:
        print('none')
    time.sleep(2)
    try:
        more = driver.find_element(By.CLASS_NAME, 'PQbOE').text.strip()
    except:
        print('none')
    try:
        more = driver.find_element(By.CLASS_NAME, 'Yy0acb').text.strip()
    except:
        print('none')
    try:
        more = driver.find_element(By.NAME, 'EvNWZc').text.strip()
    except:
        print('none')
    time.sleep(4)

    if ModuleNotFoundError:
        pass

    site_info = (name, more)
    site_list.append(site_info)

    col = ['Site Name', 'Site Link']
    df = pd.DataFrame([site_info], columns=col)
    df.to_csv("C:\\Users\LP\Documents\python\wedgwood\prada2.csv", index=False, encoding='utf-8', mode='a+')

next_page = driver.find_element(By.XPATH, '//*[@id="pnnext"]')
next_page.click()

下面是我得到的错误
回溯(最近一次调用):FILE“c:\Users\LP\Documents\python\wedgwood\wedgwood.py”,第50行,in name=card.find_ELEMENT(BYCLASS_NAME,‘OSRXXb’).ext.strie()
FIND_ELEMENT中的文件“C:\Users\LP\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py”,行返回SELF.EXECUTE(Command.FIND_CHILD_ELEMENT,
文件执行第396行,in
“C:\Users\LP\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py”,返回self.parent.ecute(命令,参数)
在EXECUTE self.error_handler.check
“C:\Users\LP\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py”,(Response)中,文件错误响应第429行。
文件“C:\Users\LP\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py”,第243行,在CHECK_RESPONSE RAY EXCEPTION_CLASS中(消息、屏幕、堆栈跟踪)
Selenium.common.exceptions.StaleElementReferenceException:消息:过时元素引用:元素未附加到页面文档

xyhw6mcr

xyhw6mcr1#

该错误意味着当它查找元素‘OSrXXb’时,它找不到它。您有一堆 Package 在try块中的其他元素,但是这个元素似乎是您期望存在的,但它并不存在。

相关问题