我一直在尝试通过Python使用Selenium来填写用户名和密码字段以允许Twitter授权。这应该是一个非常基本的任务,但它一直给我一个错误。这是我要填写的HTML中的用户名字段:
<div class="row user ">
<label for="username_or_email" tabindex="-1">Username or email</label>
<input aria-required="true" autocapitalize="off" autocorrect="off" autofocus="autofocus" class="text" id="username_or_email" name="session[username_or_email]" type="text" value="">
</div>
这是密码字段的HTML:
<div class="row password ">
<label for="password" tabindex="-1">Password</label>
<input aria-required="true" class="password text" id="password" name="session[password]" type="password" value="">
</div>
这是我的代码:
username_field = browser.find_element_by_id("username_or_email")
password_field = browser.find_element_by_id("password")
username_field.send_keys("MyEmail@gmail.com")
password_field.send_keys("SuperSecretPassword")
password_field.send_keys(Keys.ENTER)
相当简单吧?但是,当我运行代码时,它会在错误结束时尝试发送send_keys时抛出错误:
selenium.common.exceptions.WebDriverException: Message: TypeError - undefined is not a function (evaluating '_getTagName(currWindow).toLowerCase()')
任何人都可以向我解释这里会发生什么事吗?为什么拒绝填补这些字段?
答案 0 :(得分:4)
我试图在这里重现错误,但脚本运行正常
前几天我用简单的事情得到了非常奇怪的错误,解决方案是降级webdriver
我正在使用 chromedriver 2.25
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="/home/user/selenium/chromedriver2.25")
url = 'http://localhost/teste.html'
driver.get(url)
username_field = driver.find_element_by_id('username_or_email')
password_field = driver.find_element_by_id("password")
username_field.send_keys("MyEmail@gmail.com")
password_field.send_keys("SuperSecretPassword")
password_field.send_keys(Keys.ENTER)
HTML:
<div class="row user ">
<label for="username_or_email" tabindex="-1">Username or email</label>
<input aria-required="true" autocapitalize="off" autocorrect="off" autofocus="autofocus" class="text" id="username_or_email" name="session[username_or_email]" type="text" value="">
</div>
<div class="row password ">
<label for="password" tabindex="-1">Password</label>
<input aria-required="true" class="password text" id="password" name="session[password]" type="password" value="">
</div>