我正在尝试通过硒发送密钥,它需要用户名而不是密码。
我尝试单击然后发送密钥。
密码字段的HTML:
<div>
<input name="txtPassword" type="password" id="txtPassword" required="" style="margin-bottom:0px;" class="blur">
<a id="btnSmallForgotPassword" class="smallForgotPassword visible-sm-block" href="javascript:__doPostBack('btnSmallForgotPassword','')">forgot password</a>
</div>
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.ID,"txtPassword"))).click()
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.ID,"txtPassword"))).send_keys("san")
我没有收到任何错误消息,但没有发送密码密钥
答案 0 :(得分:0)
尝试使用此代码:
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')
您不应创建WebDriverWait的对象太多次,请像这样使用它:
wait = WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')
和wait.until(EC.element_to_be_clickable((By.ID,"txtPassword")))
返回一个Web元素,您可以在其中使用诸如click(), clear(), send_keys()
等的方法。
您也可以这样编写代码:
password_field = wait.until(EC.element_to_be_clickable((By.ID,"txtPassword")))
password_field.click()
password_field.send_keys('your_password')
EDIT1:
您可以使用此CSS选择器:
input[id='txtPassword']
编辑2:
您可以使用此完整方法:
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[id='txtUser']"))).send_keys("abhishek")
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')
答案 1 :(得分:0)
上面的代码应该可以工作。但是,我已经在chrome浏览器上测试了以下代码,并且工作正常。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'txtUser'))).send_keys("Abhishek")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div.divInnerRightControls #txtPassword'))).send_keys("Abhishek")
答案 2 :(得分:0)
要将字符序列发送到用户ID 和 PASSWORD 字段,您需要为 WebDriverWait 可点击的元素,您可以使用以下Locator Strategy:
代码块:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input#txtUser"))).send_keys("abhishek_gupta")
driver.find_element_by_css_selector("input#txtPassword").send_keys("abhishek_gupta")
浏览器快照:
答案 3 :(得分:0)
尝试一下:
from selenium import webdriver
import time
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get('http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7')
time.sleep(3)
username = driver.find_element_by_id("txtUser")
username.clear()
# insert username
username.send_keys("mrcats")
password = driver.find_element_by_name("txtPassword")
password.clear()
# insert password
password.send_keys("catskillz")
login = driver.find_element_by_name("btnLogin")
#click on login button
login.click()