在python中使用selenium填写用户名和密码

时间:2014-01-17 12:43:14

标签: python selenium

如何通过以下链接自动填写用户名和密码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = 'C:\\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
browser.get('http://www.example.com')

之后我真的不知道:

username = Select(browser.find_element_by_name('Username'))
password = Select(browser.find_element_by_name('Password'))
username.select_by_visible_text("text")
password.select_by_visible_text("text")

6 个答案:

答案 0 :(得分:77)

driver = webdriver.Chrome(...)

username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")

username.send_keys("YourUsername")
password.send_keys("Pa55worD")

driver.find_element_by_name("submit").click()

您的代码注释:

答案 1 :(得分:27)

使用WebElement.send_keys方法模拟键输入。

代码中的

nameUsernamePassword)与元素的实际name不匹配(usernamepassword)。


username = browser.find_element_by_name('username')
username.send_keys('user1')

password = browser.find_element_by_name('password')
password.send_keys('secret')

form = browser.find_element_by_id('loginForm')
form.submit()

# OR  browser.find_element_by_id('submit').click()

答案 2 :(得分:6)

user = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
user.clear()
user.send_keys("your_user_name")
password.clear()
password.send_keys("your_password")
driver.find_element_by_name("submit").click()

注意:

  • 我们使用user.clear()来清除输入字段。
  • 用于查找提交按钮,您可以使用基于页面源代码的任何其他方法。有关信息,请参阅locating elements

答案 3 :(得分:1)

在某些情况下,当元素不可交互时,sendKeys()不起作用,您很可能会遇到ElementNotInteractableException

在这种情况下,您可以选择执行设置值的javascript,然后可以回发。

示例:

url = 'https://www.your_url.com/'

driver = Chrome(executable_path="./chromedriver")
driver.get(url)

username = 'your_username'
password = 'your_password'

#Setting the value of email input field
driver.execute_script(f'var element = document.getElementById("email"); element.value = "{username}";')

#Setting the value of password input field
driver.execute_script(f'var element = document.getElementById("password"); element.value = "{password}";')

#Submitting the form or click the login button also
driver.execute_script(f'document.getElementsByClassName("login_form")[0].submit();')

print(driver.page_source)

参考:

https://www.quora.com/How-do-I-resolve-the-ElementNotInteractableException-in-Selenium-WebDriver

答案 4 :(得分:0)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait

# If you want to open Chrome
driver = webdriver.Chrome()
# If you want to open Firefox
driver = webdriver.Firefox()

username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")
username.send_keys("YourUsername")
password.send_keys("YourPassword")
driver.find_element_by_id("submit_btn").click()

答案 5 :(得分:-1)

我是硒的新手,我尝试了上述所有解决方案,但它们不起作用。 最后,我手动尝试了

driver = webdriver.Firefox()
import time

driver.get(url)

time.sleep(20)

print (driver.page_source.encode("utf-8"))

然后我可以从网络上获取内容。