[問題] Xpath抓不到內容

作者: garlic774 (蒜頭)   2021-11-20 20:14:52
各位大大晚安,以Xpath抓,一直結果none,請問有可能是哪邊出了問題呢?
[in]
!pip install selenium
from selenium import webdriver
import time
import pandas as pd
browser = webdriver.Chrome(executable_path='./chromedriver.exe')
browser.get("https://tw.mall.yahoo.com/store/%E5%B1%88%E8%87%A3%E6%B0%8FWatson
s:
time.sleep(5)
# 商品連結
# 如何找到rel Xpath ?
linkpath = '//ul[@class = "sc-eWvPqa cePswM"]/li/a'
product_links = browser.find_elements_by_xpath(linkpath)
product_link = product_links[0].get_attribute('href')
print('商品連結:',product_link)
[out]
IndexError Traceback (most recent call last)
<ipython-input-22-cb447ba73a6b> in <module>
11 linkpath = '//ul[@class = "sc-eWvPqa cePswM"]/li/a'
12 product_links = browser.find_elements_by_xpath(linkpath)
作者: sating00 (sating00)   2021-11-21 14:41:00
報錯不是很清楚嗎?out of range 就是你給的數超過list上限先檢查 product_links 到底有沒有東西吧
作者: garlic774 (蒜頭)   2021-11-21 19:09:00
謝謝S大 我再試試看
作者: TakiDog (多奇狗)   2021-11-21 19:29:00
用chrome debugger 搜尋看看吧盡可能不要用自動產生的xpath(判斷太嚴格)手動寫判斷不難
作者: garlic774 (蒜頭)   2021-11-21 22:13:00
後來改成 //a去搜尋,再用if in: 也篩選出來了 感謝大大們

Links booklink

Contact Us: admin [ a t ] ucptt.com