Python 硒 select 元素由 class

Python selenium select element by class

我尝试 select 一个元素与硒,但我是初学者。

这是我尝试添加的元素 select :

<button type="submit" class="btn btn-primary btn-block btn-form">
    Connexion
</button>

我在我的脚本中尝试了这几行:

from selenium import webdriver

driver = webdriver.Chrome(executable_path="chromedriver.exe")
driver.get("https://skysand.fr")

connexion_button = driver.find_element_by_class_name("login")
connexion_button.click()

email_input = driver.find_element_by_id("email")
email_input.send_keys("XXXX")

password_input = driver.find_element_by_id("password")
password_input.send_keys("XXXX")

connect_button = driver.find_element_by_class_name("btn-primary btn-block btn-form")
connect_button.click()

但它不起作用:(

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (513, 955)

如果你能帮助我,谢谢! (抱歉我的英语不好...)

为了通过多个 class 名称来 select 元素,您应该使用 css_selector 或 XPath。此外,对于此元素,最好使用此 css 定位器:

button[type='submit']

所以试试这个:

connect_button = driver.find_element_by_css_selectro("button[type='submit']")
connect_button.click()

此外,您的代码需要等待。有了它们,它看起来像这样:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

driver = webdriver.Chrome(executable_path="chromedriver.exe")
driver.maximize_window()
driver.get("https://skysand.fr")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".login"))).click()

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#email"))).send_keys("XXXX")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#password"))).send_keys("XXXX")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[type='submit']"))).click()