发送键硒python时出错:'值':keys_to_typing(value)}

时间:2018-10-26 07:27:36

标签: python selenium selenium-webdriver

我正在尝试使用硒登录网站,但是在向该网站发送用户名和密码时遇到错误:

代码:

driver = webdriver.Chrome(
executable_path='../chromedriver')

driver.get('https://secure.imdb.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.imdb.com%2Fap-signin-handler&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=imdb_pro_us&openid.mode=checkid_setup&siteState=eyJvcGVuaWQuYXNzb2NfaGFuZGxlIjoiaW1kYl9wcm9fdXMiLCJyZWRpcmVjdFRvIjoiaHR0cHM6Ly9wcm8uaW1kYi5jb20vdjIvbmFtZS9ubTM0NzUyMDk_cmY9Y29uc19ubV9tZXRlciZyZWZfPW5tX3B1Yl91cHNsYl9sb2dpbiJ9&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&imdbPageAction=login')
driver.find_element_by_id("ap_email").send_keys("username")
driver.find_element_by_id("ap_password").send_keys("pass")
driver.find_element_by_id("signInSubmit").click()

出现错误:

  

driver.find_element_by_id(“ ap_email”)。send_keys(“用户名”)     send_keys中的第479行的文件“ /anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py”       '值':keys_to_typing(value)})

Chrome版本:

  

chrome = 70.0.3538.77

chrome驱动程序版本:

  

chromedriver = 2.43.600229

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

如我所见,如果不单击"Log in with IMDb"链接,您将无法处理身份验证表单。尝试在发送密钥之前单击它:

# driver.find_element_by_id('login_with_imdb_expender').click()
driver.find_element_by_link_text('Log in with IMDb').click()
driver.find_element_by_id("ap_email").send_keys("username")
driver.find_element_by_id("ap_password").send_keys("pass")

答案 1 :(得分:1)

根据IMDb网站,在将字符字符串发送到电子邮件自定义密码字段之前,您必须先点击 link (文本为使用IMDb登录登录),并引发 WebDriverWait ,您可以使用以下解决方案:

代码块:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://secure.imdb.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.imdb.com%2Fap-signin-handler&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=imdb_pro_us&openid.mode=checkid_setup&siteState=eyJvcGVuaWQuYXNzb2NfaGFuZGxlIjoiaW1kYl9wcm9fdXMiLCJyZWRpcmVjdFRvIjoiaHR0cHM6Ly9wcm8uaW1kYi5jb20vdjIvbmFtZS9ubTM0NzUyMDk_cmY9Y29uc19ubV9tZXRlciZyZWZfPW5tX3B1Yl91cHNsYl9sb2dpbiJ9&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&imdbPageAction=login')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='a-expander-prompt']/span[@id='login_with_imdb_expender']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='a-input-text a-span12 auth-autofocus auth-required-field' and @id='ap_email']"))).send_keys("aravind_reddy")
driver.find_element_by_xpath("//input[@class='a-input-text a-span12 auth-required-field' and @id='ap_password']").send_keys("aravind_reddy")
driver.find_element_by_xpath("//input[@class='a-button-input' and @id='signInSubmit']").click()