填写后如何使用 selenium python 提交 angular 表单

How to submit an angular form with selenium python after filling out

<button ng-click="LoginForm.login($event)" id="login_btn" ng-disabled="LoginForm.isSubmitting" type="submit" class="btn btn-info btn-large btn-submit ModalLoginSignup-loginForm-submitBtn" fl-analytics="NormalLogin" i18n-id="3bbbad631029e3575da7a151bba4f37c" i18n-msg="Log In">
                    Log In
                </button>

这就是我正在尝试的。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.implicitly_wait(5)
driver.maximize_window()
driver.get("https://xxxxxxxxx.com")
user = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
user.clear()
user.send_keys("xxxxxxxx")
password.clear()
password.send_keys("xxxxxxx")
driver.implicitly_wait(5)
# password.send_keys(u'\ue007')
# driver.click()
# driver.implicitly_wait(30)
# driver.find_element_by_id("login_btn").send_keys(u'\ue007')
# driver.find_element_by_id('login_btn').send_keys(Keys.ENTER)
# login_button = driver.find_element_by_xpath("")



driver.find_element_by_xpath("//[@id='login_btn']").send_keys(u'\ue007')
# login_button.submit()
# driver.find_element_by_css_selector(".ModalLoginSignup-loginForm-submitBtn").submit()

根据您共享的 HTML,所需的元素是一个 Angular 元素,因此要在该元素上调用 click(),您必须引入 WebDriverWait 使 元素可点击 并且您可以使用以下任一解决方案:

  • CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-submit.ModalLoginSignup-loginForm-submitBtn#login_btn"))).click()
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-info btn-large btn-submit ModalLoginSignup-loginForm-submitBtn' and @id='login_btn']"))).click()
    
  • 注意:您必须添加以下导入:

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