python - 使用硒的谷歌帐户登录自动化导致错误
问题描述
我正在尝试使用 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()
关于做什么的任何建议?
解决方案
问题是,我只需要等待密码存在和可见性被加载,如下所示:
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)
推荐阅读
- rest - OData v4 扩展多个一对多
- flutter - 文本溢出时如何淡化文本?
- r - R: as.name() - 不支持的类型符号错误”
- bluetooth - 如何检查 BLE 广告是否已启用(HCI)
- c# - GitLab 提供了错误的 C# 代码编码。解决此问题的方法
- amazon-web-services - 如何使用 CLI 访问 ec2 windows 实例中的 s3 存储桶
- quill.io - 是否可以使用 IN 使用 Quill 更新多行
- typescript - Pulumi 在运行时获取 kubernetes 上下文
- r - 表示每行按列组,循环
- google-apps-script - 从编码的谷歌表单中获取响应