【问题标题】:Error: 'value': keys_to_typing(value)} while sending keys selenium python错误:'value':keys_to_typing(value)} 发送密钥 selenium python
【发布时间】:2019-03-30 21:51:57
【问题描述】:

我正在尝试使用 selenium 登录网站,但在向网站发送用户名和密码时出错:

代码:

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("username") 文件“/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py”,第 479 行,在 send_keys “值”:keys_to_typing(value)})

镀铬版本:

铬=70.0.3538.77

chrome 驱动版本:

chromedriver=2.43.600229

如何解决?

【问题讨论】:

  • 请将完整错误回溯作为代码格式文本添加到您的问题中。

标签: python selenium selenium-webdriver


【解决方案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")

【讨论】:

  • 感谢很好的回答:)。我尝试了很多东西,但这并没有引起我的注意,因为在单击它之前我也能够找到元素(我将它打印在控制台上)但是当时它没有工作
【解决方案2】:

根据IMDb 网站,在将字符串 发送到电子邮件自定义密码 字段之前,您必须点击link 与文本为 Login with 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()

【讨论】:

  • 您能否解释一下为什么 OP 在这种情况下需要使用 WebDriverWait 以及为什么应该使用过于复杂的定位器而不是简单的定位器,因为与我的答案相比,您的答案没有任何好处?跨度>
猜你喜欢
  • 1970-01-01
  • 2018-10-02
  • 2018-09-05
  • 2016-08-05
  • 1970-01-01
  • 2022-11-10
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多