python - 密码字段不通过 Python 使用 Selenium 获取 key

标签 python selenium selenium-webdriver css-selectors webdriverwait

我正在尝试通过 selenium 发送 key ,它用于用户名而不是密码。

然后我尝试过单击并发送 key 。

密码字段的 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")

我没有收到错误消息,但它没有发送密码 key

最佳答案

尝试使用此代码:

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

编辑1:

您可以使用此 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')

关于python - 密码字段不通过 Python 使用 Selenium 获取 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56374602/

相关文章:

python - 正则表达式获取双引号之间的所有内容

selenium - 如何使用 sibling 编写XPath?

java - 线程 "main"org.openqa.selenium.ElementNotInteractableException : element not interactable 中出现异常

python - 为什么在Dockerfile中使用requirements.txt中的 pip `install_requires`从setup.py安装 “.”失败?

python - 从旧 Python 版本到 Python 3 的高阶函数

testing - Selenium 服务器-firefoxProfileTemplate 无法工作

java - Selenium:getPageSource() 返回前一页的来源

java - Selenium:将 WebElement 添加到列表中,然后检查所有元素的可见性

selenium - 无法单击元素 "Search Intimation View-Details"抛出没有此类元素异常

python - 如何教 SQLAlchemy 从断开连接中恢复?