我正在使用 Python Selenium 打開https://www.walmart.com/然后想要單擊“登錄帳戶”下的“創建帳戶”選項卡,如沃爾瑪網站圖片所示。沃爾瑪網站上的按鈕源代碼及圖片如下:
<a link-identifier="Create an account" class="no-underline" href="/account/signup?vid=oaoh">
<button class="w_C8 w_DB w_DE db mb3 w-100" type="button" tabindex="-1">Create an account</button>
</a>
沃爾瑪網站原始碼-圖片
我打開https://www.walmart.com/以訪問“創建帳戶”按鈕并單擊它的python 代碼如下:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
url = "https://www.walmart.com/"
s=Service('C:/Users/Samiullah/.wdm/drivers/chromedriver/win32/96.0.4664.45/chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get(url)
driver.maximize_window()
elems = driver.find_element(By.XPATH, '//button[normalize-space()="Create an account"]')
time.sleep(3)
elems.click()
但是,我收到了 ElementNotInteractableException 的此錯誤,如此錯誤圖片所示。
誰能指導我的代碼/方法有什么問題?
提前致謝
uj5u.com熱心網友回復:
您不能直接單擊此元素,您必須將滑鼠懸停在“登錄”元素上才能顯示此元素。
還強烈建議使用預期條件。
這應該有效:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
url = "https://www.walmart.com/"
s=Service('C:/Users/Samiullah/.wdm/drivers/chromedriver/win32/96.0.4664.45/chromedriver.exe')
driver = webdriver.Chrome(service=s)
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get(url)
driver.maximize_window()
sign_in_btn = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Sign In']")))
actions.move_to_element(sign_in_btn).perform()
time.sleep(0.5)
wait.until(EC.visibility_of_element_located((By.XPATH, '//button[normalize-space()="Create an account"]'))).click()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/406762.html
標籤:
