首页 > 解决方案 > 使用硒的谷歌帐户登录自动化导致错误

问题描述

我正在尝试使用 selenium 登录 Google。步骤很简单,首先输入电子邮件并点击下一步,然后输入密码并点击下一步。我的代码如下所示:

driver = webdriver.Firefox()
driver.get("https://accounts.google.com/signin")

driver.implicitly_wait(3)

driver.find_element_by_id("identifierId").send_keys("email")
driver.find_element_by_id("identifierNext").click()

password = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[1]/div/form/content/section/div/content/div[1]/div/div[1]/div/div[1]/input")
password.send_keys("password")

element = driver.find_element_by_id('passwordNext')
driver.execute_script("arguments[0].click();", element)

代码的第一部分(电子邮件部分)完美运行。我还检查了最后一部分(在写入密码后点击下一步),它也很好用。唯一的问题是密码。当我尝试这样做password.send_keys("password")时会导致以下错误:

TypeError: object of type 'FirefoxWebElement' has no len()

关于做什么的任何建议?

标签: pythonseleniumerror-handlingautomationgoogle-signin

解决方案


问题是,我只需要等待密码存在和可见性被加载,如下所示:

driver = webdriver.Firefox()
driver.get("https://accounts.google.com/signin")

driver.implicitly_wait(3)

driver.find_element_by_id("identifierId").send_keys("email")
driver.find_element_by_id("identifierNext").click()

driver.implicitly_wait(8)
password = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@type='password']")))
password = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@type='password']")))

password = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[1]/div/form/content/section/div/content/div[1]/div/div[1]/div/div[1]/input")
password.send_keys("password")

element = driver.find_element_by_id('passwordNext')
driver.execute_script("arguments[0].click();", element)

推荐阅读