[英]How do I acces the products element in a website with shadow-root?
I am new to python and I am trying to learn.我是 python 新手,我正在努力学习。 I am currently doing a web scraping project on a website that has a shadow-root, but I have trouble accessing the elements under the shadow-root.
我目前正在一个具有 shadow-root 的网站上进行网络抓取项目,但我无法访问 shadow-root 下的元素。 I would really appreciate a bit of help and some advice.
我真的很感激一些帮助和一些建议。 The program is 'untitled0.py'.
该程序是“untitled0.py”。 Also, I am trying to only use selenium webdriver, and as a last resort shadowpy.
另外,我试图只使用 selenium webdriver,并作为最后的手段 shadowpy。 Whatever I did,
product_name_TOS
returned an empty list.无论我做什么,
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)
Almost everything is correct, there is just a small error in几乎所有内容都是正确的,只是有一个小错误
product_name_TOS = product_container_TOS.find_element("class name", "prodTitle").get_element("innerText")
The method is not .get_element
but .get_attribute
.该方法不是
.get_element
而是.get_attribute
。 Notice that in this case you can also use .text
请注意,在这种情况下,您还可以使用
.text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.