我有 1 個問題,我無法使用 javascript 單擊按鈕。我用
driver.find_element(By.XPATH, "//li[@class='login']/a")
但它根本不起作用。它看起來像 javascripts。

請幫我點擊登錄按鈕。

uj5u.com熱心網友回復:
如果您可以提供更多的源代碼或網站的鏈接,這可能會更有幫助?
但就我所見。你真的點擊了嗎?
driver.find_element(By.XPATH, "//li[@class='login']/a").click()
uj5u.com熱心網友回復:
您需要等待元素可點擊(記得添加適當的匯入),這是一個作業代碼:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.oliveyoung.co.kr")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='login']/a"))).click()
input()
driver.close()
uj5u.com熱心網友回復:
請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
您應該檢查的 xpath :
//li[@class='login']//a
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上xpath看看,如果你需要的element是越來越強調與1/1匹配的節點。
如果我們有 1/1 匹配節點,請確保:
- 此 div 不在 iframe 下。
- 此 div 不在 shadow-root 下。
- 您不應該在 selenium 啟動的新選項卡/視窗上。
如果它匹配所需的元素,那么在 Selenium 中有 4 種方法可以點擊它。
代碼試用1:
time.sleep(5)
driver.find_element_by_xpath("//li[@class='login']//a").click()
代碼試用2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='login']//a"))).click()
代碼試用3:
time.sleep(5)
button = driver.find_element_by_xpath("//li[@class='login']//a")
driver.execute_script("arguments[0].click();", button)
代碼試用4:
time.sleep(5)
button = driver.find_element_by_xpath("//li[@class='login']//a")
ActionChains(driver).move_to_element(button).click().perform()
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/351365.html
