I am new to python and I am trying to learn. 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. I would really appreciate a bit of help and some advice. The program is 'untitled0.py'. Also, I am trying to only use selenium webdriver, and as a last resort shadowpy. Whatever I did, product_name_TOS
returned an empty list.
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)
CodePudding user response:
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
. Notice that in this case you can also use .text