[英]How do I acces the products element in a website with shadow-root?
我是 python 新手,我正在努力學習。 我目前正在一個具有 shadow-root 的網站上進行網絡抓取項目,但我無法訪問 shadow-root 下的元素。 我真的很感激一些幫助和一些建議。 該程序是“untitled0.py”。 另外,我試圖只使用 selenium webdriver,並作為最后的手段 shadowpy。 無論我做什么, product_name_TOS
返回一個空列表。
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
service = Service(executable_path=ChromeDriverManager().install())
driver_TOS = webdriver.Chrome(service=service)
driver_TOS.get("https://www.bedbathandbeyond.com/store/category/college/decor/10625?icid=hp_homepage_4acrs_slot2_bath")
prod_names_TOS = []
prod_prices_TOS = []
time.sleep(5)
shadow_host_TOS = driver_TOS.find_element("id", "wmHostPrimary")
shadow_root = driver_TOS.execute_script('return arguments[0].shadowRoot', shadow_host_TOS)
#shadow_root_TOS = shadow_host_TOS.shadowRoot
products_TOS = shadow_root.find_elements("css selector", ".prodCardWrap")
for product_TOS in products_TOS:
product_container_TOS = product_TOS.find_element("class name", "prodCardR")
product_name_TOS = product_container_TOS.find_element("class name", "prodTitle").get_element("innerText")
prod_names_TOS.append(product_name_TOS)
print(prod_names_TOS)
幾乎所有內容都是正確的,只是有一個小錯誤
product_name_TOS = product_container_TOS.find_element("class name", "prodTitle").get_element("innerText")
該方法不是.get_element
而是.get_attribute
。 請注意,在這種情況下,您還可以使用.text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.