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

x6h2sr28  于 2022-12-13  发布在  Python
关注(0)|答案(7)|浏览(318)

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

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

camsedfj1#

文件:https://selenium-python.readthedocs.io/navigating.html
对于版本4.3.0(2022年6月发布)及更高版本,Selenium中删除了对find_element_by_*find_elements_by_*的调用。

from selenium.webdriver.common.by import By

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

# To catch <input type="text" id="passwd" />
password = driver.find_element(By.ID, "passwd")
# To catch <input type="text" name="passwd" />
password = driver.find_element(By.NAME, "passwd")

password.send_keys("Pa55worD")

driver.find_element(By.NAME, "submit").click()

对于API版本4.2.0或更早版本,原始响应如下:

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

代码注解:Select()用于作用于选择元素(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select)。

vohkndzv

vohkndzv2#

使用WebElement.send_keys方法模拟按键输入。
代码(UsernamePassword)中的name与元素(usernamepassword)的实际name不匹配。

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

pftdvrlh3#

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
mwg9r5ms

mwg9r5ms4#

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

camsedfj

camsedfj5#

以下是完整的答案。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

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

username = browser.find_element(By.NAME, 'Username')
password = browser.find_element(By.NAME, 'Password')

username.send_keys("yourUsername") #type your own username here
password.send_keys("yourPassword") #type your own password here

browser.find_element(By.NAME, 'submit').click()

由于不赞成使用find_element_by_name(),因此可以使用find_element(By.NAME, 'name')
您还必须导入from selenium.webdriver.common.by import By

xkrw2x1b

xkrw2x1b6#

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

wlzqhblo7#

我是 selenium 的新手,我尝试了上面所有的解决方案,但都不起作用。最后,我手动尝试了这个

driver = webdriver.Firefox()
import time

driver.get(url)

time.sleep(20)

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

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

相关问题