Selenium:无法通过 Xpath 找到并单击元素

Selenium :Cant find and click element by X-path

我正在尝试使用 selenium 单击网站上的按钮。但是硒找不到它。这不是 iframe 或 X-path 的问题。我认为有一些事件可以使元素在 javascript 之前可用。我尝试使用 class 获取它,尽管它的 class 值很差。如果有帮助的话。这对我来说是一种解脱。我已经尽力了。我从 firefox 开发人员 tools.The url 那里得到了 X 路径,站点是:https://www.bedbathandbeyond.com/store/category/kitchen/trash-recycling/14367 我想点击“下一步”按钮(几乎在底部)

from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

option = webdriver.ChromeOptions()
option.add_argument("start-maximized")
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-blink-features")
option.add_argument("--disable-gpu")
#option.add_argument("--headless")
option.add_argument('--disable-blink-features=AutomationControlled')
wd = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=option)
wait = WebDriverWait(wd, 40)

wd.get('https://www.bedbathandbeyond.com/store/category/kitchen/trash-recycling/14367')
wait.until(EC.element_to_be_clickable((By.XPATH, "/body/div[3]/div[8]/div[1]/div[3]/div[2]/amp-list/div/div/button[2]")))
wd.find_element(By.XPATH,'/body/div[3]/div[8]/div[1]/div[3]/div[2]/amp-list/div/div/button[2]').click()
# class="plpPage plpNext flex mid pwaOnly "
time.sleep(15)
wd.quit()

编辑: 我认为这是一些阴影根。我不太了解但是尝试了这段代码但是没有用

def expand_shadow_element(element):
  shadow_root = wd.execute_script('return arguments[0].shadowRoot', element)
  return shadow_root
wd.get('https://www.bedbathandbeyond.com/store/category/kitchen/trash-recycling/14367')
wait.until(EC.element_to_be_clickable((By.XPATH, '/body/div[3]/div[8]/div[1]/div[3]/div[2]/amp-list/div/div')))
ele = wd.find_element(By.XPATH, '/body/div[3]/div[8]/div[1]/div[3]/div[2]/amp-list/div/div')
root = expand_shadow_element(ele)

等待时出现超时错误

您可以改用 javascript..这将单击下一步按钮

driver.execute_script(
    'document.querySelector("#wmHostPrimary").shadowRoot.querySelector("button.plpPage.plpNext.flex.mid.pwaOnly").click()')