简体   繁体   English

如何使用 shadow-root 访问网站中的产品元素?

[英]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.

相关问题 如何定位在 shadow-root 中找不到的元素 - How to locate element not found in shadow-root 网页抓取#shadow-root - Webscraping #shadow-root 有没有办法从网站上的shadow-root中提取信息? - Is there a way to extract information from shadow-root on a Website? How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python - How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python 如何阅读#shadow-root(用户代理)下的文本 - How to read text that is under #shadow-root (user-agent) Python Selenium can't find element by xpath within #shadow-root (open) using Selenium and Python - Python Selenium can't find element by xpath within #shadow-root (open) using Selenium and Python 将 Google Chrome 升级到 96 版后,shadow-root 元素搜索不起作用 - After upgrading to Google Chrome to version 96, shadow-root element search does not work python selenium:无法定位元素,shadow-root内的svg-icon按钮 - python selenium: unable to locate element, svg-icon button inside shadow-root 无法使用 Selenium 和 Python 在 #shadow-root(打开)中找到登录元素 - Unable to locate the Sign In element within #shadow-root (open) using Selenium and Python 如何使用 Selenium 在#shadow-root(打开)中切换到子框架 - How to switch to the child frame within #shadow-root (open) using Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM