【问题标题】:Selenium Web Scraping: Find element by text not working in scriptSelenium Web Scraping:按文本查找元素在脚本中不起作用
【发布时间】:2022-01-09 19:48:32
【问题描述】:

我正在编写一个脚本来收集来自 Newegg 的信息,以查看显卡价格随时间的变化。目前,我的脚本将通过 Chromedriver 在 RTX 3080 上打开 Newegg 搜索,然后单击桌面显卡的链接以缩小搜索范围。我正在努力的部分是开发一个 for item in range 循环,它可以让我遍历所有 8 个搜索结果页面。我知道我可以通过简单地更改 URL 中的页码来做到这一点,但由于这是一个我试图用来更好地学习相对 Xpath 的练习,我想使用底部的分页按钮来做到这一点页。我知道每个按钮都应该包含“1、2、3、4等”的内部文本。但是每当我在 for 循环中使用 text() = {item} 时,它都不会单击按钮。该脚本运行并且不返回任何异常,但也没有执行我想要的操作。下面我附上了页面的 HTML 以及我当前的脚本。任何建议或提示表示赞赏。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import pandas as pd
import time

options = Options()

PATH = 'C://Program Files (x86)//chromedriver.exe'

driver = webdriver.Chrome(PATH)

url = 'https://www.newegg.com/p/pl?d=RTX+3080'

driver.maximize_window()
driver.get(url)

card_path = '/html/body/div[8]/div[3]/section/div/div/div[1]/div/dl[1]/dd/ul[2]/li/a'
desktop_graphics_cards = driver.find_element(By.XPATH, card_path)
desktop_graphics_cards.click()
time.sleep(5)

graphics_card = []
shipping_cost = []
price = []
total_cost = []

for item in range(9):
    try:
        #next_page_click = driver.find_element(By.XPATH("//button[text() = '{item + 1}']"))
        print(next_page_click)
        next_page_click.click()
    except:
        pass

【问题讨论】:

    标签: python selenium for-loop xpath selenium-chromedriver


    【解决方案1】:

    分页按钮超出了最初可见的区域。
    要点击这些元素,您必须滚动页面直到元素出现。
    此外,当您尝试使用从 1 到 9 的数字时,您需要单击从 2 到 9(包括)的下一页按钮。
    我认为这应该会更好:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import NoSuchElementException
    import pandas as pd
    import time
    
    options = Options()
    
    PATH = 'C://Program Files (x86)//chromedriver.exe'
    
    driver = webdriver.Chrome(PATH)
    
    url = 'https://www.newegg.com/p/pl?d=RTX+3080'
    actions = ActionChains(driver)
    
    driver.maximize_window()
    driver.get(url)
    
    card_path = '/html/body/div[8]/div[3]/section/div/div/div[1]/div/dl[1]/dd/ul[2]/li/a'
    desktop_graphics_cards = driver.find_element(By.XPATH, card_path)
    desktop_graphics_cards.click()
    time.sleep(5)
    
    graphics_card = []
    shipping_cost = []
    price = []
    total_cost = []
    
    for item in range(2,10):
        try:
            next_page_click = driver.find_element(By.XPATH(f"//button[text() = '{item}']"))
            actions.move_to_element(next_page_click).perform()
            time.sleep(2)
            #print(next_page_click) - printing a web element itself will not give you usable information
            next_page_click.click()
            #let the next page loaded, it takes some time
            time.sleep(5)
        except:
            pass
    

    【讨论】:

    • 感谢@Prophet 的建议!不幸的是,我仍然得到相同的结果,并且脚本永远不会超出第一页。您是否有任何其他建议,或者我是否需要提供一些其他信息,以使我做错的事情更加清楚?据我所知,由于某种原因,脚本不会向下滚动到底部的按钮。
    • 去掉except看看抛出了什么错误
    • 看起来它返回一个类型错误:next_page_click = driver.find_element(By.XPATH("//button[text() = '{item}']")) TypeError: 'str'对象不可调用。我想我需要使用不同的 XPATH 吗?我不确定哪一个能让我获得可点击按钮
    • 我们忘记了“f-strings”f... :) 让我知道它现在是否工作
    • 完美运行,谢谢!
    猜你喜欢
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2018-12-06
    • 2021-09-20
    相关资源
    最近更新 更多