我正在尝试使用硒登录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()
关于做什么的任何建议?
答案 0 :(得分:1)
问题是,我只需要等待密码的有效性和可见性被加载,就像这样:
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)