我正在嘗試使用 selenium 單擊網站上的按鈕。但是硒找不到它。這不是 iframe 或 X-path 的問題。我認為有一些事件可以通過 javascript 使元素可用。我嘗試使用類來獲取它,盡管它的類值非常差。如果有可以幫助的。這對我來說是一種解脫。我已經盡力了。我從 firefox 開發者工具中獲得了 X-path。網站的網址是: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()
#
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)
它在等待時給出超時錯誤
uj5u.com熱心網友回復:
你可以改用javascript..這將點擊下一步按鈕
driver.execute_script(
'document.querySelector("#wmHostPrimary").shadowRoot.querySelector("button.plpPage.plpNext.flex.mid.pwaOnly").click()')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/455207.html
