我無法在其他頁面中獲得其他“href”,但我仍然可以在第一頁中獲得“href”。它出什么問題了?如果我更改 Xpath 是否可以在所有頁面中獲得總“href”?
!pip install selenium
from selenium import webdriver
import time
import pandas as pd
browser = webdriver.Chrome(executable_path='./chromedriver.exe')
browser.implicitly_wait(5)
# https://tw.mall.yahoo.com/store/屈臣氏Watsons:watsons (original page)
url = "https://tw.mall.yahoo.com/search/product?p=屈臣氏&pg=2"
browser.get(url)
# 商品連結
# 如何取得正確的Xpath ?
linkPath = "//section[contains(@class,'MainListing__StoreBoothWrap')]/div/div/div/ul/li/a"
product_links = browser.find_elements_by_xpath(linkPath)
print(len(product_links))
for link in product_links:
print(link.get_attribute("href"))
uj5u.com熱心網友回復:
您可以使用以下定位器來提取產品鏈接。
X路徑:
//ul[@class='gridList']/li/a
CSS - 選擇器:
ul.gridList > li > a
編碼:
# Imports Required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver,30)
for i in range(1,5): # Iterate from page 1 to 4
driver.get("https://tw.mall.yahoo.com/search/product?p=屈臣氏&pg={}".format(i))
# Wait Until the product appear
wait.until(EC.presence_of_element_located((By.XPATH,"//ul[@class='gridList']")))
# Get the products
product_links = driver.find_elements(By.XPATH,"//ul[@class='gridList']/li/a")
# Iterate over 'product_links' to get all the 'href' values
for j,link in zip(range(len(product_links)),product_links):
print(f"{j} : {link.get_attribute('href')}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/366543.html
上一篇:selenium.common.exceptions.ElementNotInteractableException:訊息:切換框架后元素不可互動
