如何通过以下链接自动填写用户名和密码:
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")
答案 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()
您的代码注释:
find_element_by_name('Username')
:Username
大写与任何内容都不匹配。Select()
用于操作选择元素(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select)答案 1 :(得分:27)
使用WebElement.send_keys
方法模拟键输入。
name
(Username
,Password
)与元素的实际name
不匹配(username
,password
)。
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()
来清除输入字段。答案 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"))
然后我可以从网络上获取内容。