我正在撰寫一個腳本來收集 Newegg 的資訊,以查看顯卡價格隨時間的變化。目前,我的腳本將通過 Chromedriver 在 RTX 3080 上打開 Newegg 搜索,然后單擊桌面顯卡的鏈接以縮小搜索范圍。我正在努力的部分是開發一個 for item in range 回圈,它可以讓我遍歷所有 8 個搜索結果頁面。我知道我可以通過簡單地更改 URL 中的頁碼來做到這一點,但由于這是一個我試圖用來更好地學習相對 Xpath 的練習,我想使用底部的分頁按鈕來做到這一點頁。我知道每個按鈕都應該包含“1、2、3、4等”的內部文本。但是每當我在 for 回圈中使用 text() = {item} 時,它都不會單擊按鈕。腳本運行并沒有 t 回傳任何例外,但也不做我想要的。下面我附上了頁面的 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
uj5u.com熱心網友回復:
分頁按鈕不在最初可見的區域。
為了單擊這些元素,您必須滾動頁面直到元素出現。
此外,當您嘗試使用從 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408652.html
標籤:
