在python中使用硒填充用户名和密码


79

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

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")

Answers:


110
driver = webdriver.Firefox(...)  # Or Chrome(), or Ie(), or Opera()

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()

代码注释:


3
嘿,您知道提交表单后如何阅读已加载页面的内容吗?我正在为一个网站编写测试,并且设法提交了表格,但现在我迷路了。换句话说,我想获取受限制的成员唯一登陆页面的内容。
NinoŠkopac'17

3
@NinoŠkopac“硒”对象的作用类似于浏览器。一旦“单击”,它将加载新的登录页面,因此您可以执行“ selenium.find_element_by_id(“随便什么”)'。另请阅读有关“ selenium.implicitly_wait()”的信息,以在页面加载之间等待。
xbello

2
请注意,在上述答案中,硒应该是获得的驱动程序句柄,例如selenium = webdriver.Firefox()
Yu Shen

@xbello非常感谢!
Sohan Das

28

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

name在代码(UsernamePassword)不匹配实际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()

当我尝试这个,我得到这个错误:“StatefulBrowser”对象有没有属性“find_element_by_name”
安娜克劳迪亚

1
@AnaClaudia,自从我发布此答案以来,api可能已更改。请检查文档。
falsetru

9
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()用来清除输入字段。
  • 要查找“提交”按钮,可以使用基于页面源代码的任何其他方法。有关信息,请参见定位元素

4

在某些情况下,该元素不可交互,将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


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()
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.