使用 Python-Selenium 自动登录 GMAIL

2023-12-30

我正在尝试使用 Python 的 Selenium 包自动登录 GMail。但是,我无法完成任务并收到以下错误:

Traceback (most recent call last):
  File "C:\Users\Surojit\Desktop\Python\automaticpasswordFiller.py", line   21, in <module>
    passwordElem = browser.find_element_by_id('Passwd')
  File "C:\Users\Surojit\AppData\Local\Programs\Python\Python35-32\lib\site- packages\selenium\webdriver\remote\webdriver.py", line 266, in  find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\Surojit\AppData\Local\Programs\Python\Python35-32\lib\site- packages\selenium\webdriver\remote\webdriver.py", line 744, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Users\Surojit\AppData\Local\Programs\Python\Python35-32\lib\site-  packages\selenium\webdriver\remote\webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Surojit\AppData\Local\Programs\Python\Python35-32\lib\site- packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate  element: {"method":"id","selector":"Passwd"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_   (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@goo glecode.com/components/driver-component.js:10770)
     at FirefoxDriver.prototype.findElement   (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@goo  glecode.com/components/driver-component.js:10779)
    at DelayedCommand.prototype.executeInternal_/h   (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@goo    glecode.com/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_    (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@goo glecode.com/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/<   (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/[email protected] /cdn-cgi/l/email-protection/components/command-processor.js:12608) 

我写的简单代码是:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

browser = webdriver.Firefox()
browser.get('http://gmail.com')
action = webdriver.ActionChains(browser)
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys("MyUserName")
browser.find_element_by_name('signIn').click()
#browser.get('https://accounts.google.com/ServiceLogin?         service=mail&continue=https://mail.google.com/mail/#password')
passwordElem = browser.find_element_by_id('Passwd')
passwordElem.send_keys("MyPassword")
browser.find_element_by_name('signIn').click()

另外,我尝试通过将代码与此处类似问题的答案进行比较来找出代码中的错误:使用 Python Selenium 自动连接我的 Gmail 帐户 https://stackoverflow.com/questions/33708821/auto-connect-on-my-gmail-account-with-python-selenium

有人可以引导我走上正确的道路并让我知道我在哪里犯了错误吗?

PS:这是我在 stackoverflow 上的第一篇文章。请原谅我在发布问题时犯的任何错误


您正在尝试寻找Passwd尚未加载到 dom 中的元素的 id。尝试添加一些延迟以便页面可以加载。

emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('MyUserName')
nextButton = browser.find_element_by_id('next')
nextButton.click()
time.sleep(1)
passwordElem = browser.find_element_by_id('Passwd')
passwordElem.send_keys('MyPassword')
signinButton = browser.find_element_by_id('signIn')
signinButton.click()

推荐的方法是browser.implicitly_wait(num_of_seconds) see this http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-after-a-click.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python-Selenium 自动登录 GMAIL 的相关文章

  • Gunicorn 工作人员无论如何都会超时

    我正在尝试通过gunicorn运行一个简单的烧瓶应用程序 但是无论我做什么 我的工作人员都会超时 无论是否有针对应用程序的活动 工作人员在我设置任何内容后总是会超时timeout值到 是什么导致它们超时 当我发出请求时 请求成功通过 但工作
  • 为什么 dataclasses.astuple 返回类属性的深层副本?

    在下面的代码中astuple函数正在执行数据类的类属性的深层复制 为什么它不能产生与函数相同的结果my tuple import copy import dataclasses dataclasses dataclass class Dem
  • 在 Python 中将列表元素作为单独的项目返回

    Stackoverflow 的朋友们大家好 我有一个计算列表的函数 我想单独返回列表的每个元素 如下所示 接收此返回的函数旨在处理未定义数量的参数 def foo my list 1 2 3 4 return 1 2 3 4 列表中的元素数
  • matplotlib 图中点的标签

    所以这是一个关于已发布的解决方案的问题 我试图在我拥有的 matplotlib 散点图中的点上放置一些数据标签 我试图在这里模仿解决方案 是否有与 MATLAB 的 datacursormode 等效的 matplotlib https s
  • Tensorboard SyntaxError:语法无效

    当我尝试制作张量板时 出现语法错误 尽管开源代码我还是无法理解 我尝试搜索张量板的代码 但不清楚 即使我不擅长Python 我这样写路径C Users jh902 Documents logs因为我正在使用 Windows 10 但我不确定
  • Python 内置的 super() 是否违反了 DRY?

    显然这是有原因的 但我没有足够的经验来认识到这一点 这是Python中给出的例子docs http docs python org 2 library functions html super class C B def method se
  • 当字段是数字时怎么说...在 mongodb 中匹配?

    所以我的结果中有一个名为 城市 的字段 结果已损坏 有时它是一个实际名称 有时它是一个数字 以下代码显示所有记录 db zips aggregate project city substr city 0 1 sort city 1 我需要修
  • 尽管我已在 python ctypes 中设置了信号处理程序,但并未调用它

    我尝试过使用 sigaction 和 ctypes 设置信号处理程序 我知道它可以与python中的信号模块一起使用 但我想尝试学习 当我向该进程发送 SIGTERM 时 但它没有调用我设置的处理程序 只打印 终止 为什么它不调用处理程序
  • Python - 如何确定解析的 XML 元素的层次结构级别?

    我正在尝试使用 Python 解析 XML 文件中具有特定标记的元素并生成输出 excel 文档 该文档将包含元素并保留其层次结构 我的问题是我无法弄清楚每个元素 解析器在其上迭代 的嵌套深度 XML 示例摘录 3 个元素 它们可以任意嵌套
  • 将 Matlab 的 datenum 格式转换为 Python

    我刚刚开始从 Matlab 迁移到 Python 2 7 在读取 mat 文件时遇到一些问题 时间信息以 Matlab 的日期数字格式存储 对于那些不熟悉它的人 日期序列号将日历日期表示为自固定基准日期以来已经过去的天数 在 MATLAB
  • 如何使用 Python 3 检查目录是否包含文件

    我到处寻找这个答案但找不到 我正在尝试编写一个脚本来搜索特定的子文件夹 然后检查它是否包含任何文件 如果包含 则写出该文件夹的路径 我已经弄清楚了子文件夹搜索部分 但检查文件却难倒了我 我发现了有关如何检查文件夹是否为空的多个建议 并且我尝
  • PySpark groupByKey 返回 pyspark.resultiterable.ResultIterable

    我试图找出为什么我的 groupByKey 返回以下内容 0
  • 为什么 csv.DictReader 给我一个无属性错误?

    我的 CSV 文件是 200 Service 我放入解释器的代码是 snav csv DictReader open screennavigation csv delimiter print snav fieldnames 200 for
  • Firebase Firestore:获取文档的生成 ID (Python)

    我可以创建一个新文档 带有自动生成的 ID 并存储对其的引用 如下所示 my data key value doc ref db collection u campaigns add my data 我可以像这样访问数据本身 print d
  • 如何使用 Boto3 启动具有 IAM 角色的 EC2 实例?

    我无法弄清楚如何使用指定的 IAM 角色在 Boto3 中启动 EC2 实例 以下是迄今为止我如何成功创建实例的一些示例代码 import boto3 ec2 boto3 resource ec2 region name us west 2
  • 根据 Pandas 中的列表选择数据框行的子集

    我有一个数据框df1并列出x In 22 import pandas as pd In 23 df1 pd DataFrame C range 5 B range 10 20 2 A list abcde In 24 df1 Out 24
  • 如何在 Flask 中的视图函数/会话之间传递复杂对象

    我正在编写一个 Web 应用程序 当 且仅当 用户登录时 该应用程序从第三方服务器接收大量数据 这些数据被解析为自定义对象并存储在list 现在 用户在应用程序中使用这些数据 调用不同的视图 例如发送不同的请求 我不确定什么是最好的模式在视
  • 将索引与值交换的最快方法

    考虑pd Series s s pd Series list abcdefghij list ABCDEFGHIJ s A a B b C c D d E e F f G g H h I i J j dtype object 交换索引和值并
  • pytest找不到模块[重复]

    这个问题在这里已经有答案了 我正在关注pytest 良好实践 https docs pytest org en latest explanation goodpractices html test discovery或者至少我认为我是 但是
  • 如何将Python3设置为Mac上的默认Python版本?

    有没有办法将 Python 3 8 3 设置为 macOS Catalina 版本 10 15 2 上的默认 Python 版本 我已经完成的步骤 看看它安装在哪里 ls l usr local bin python 我得到的输出是这样的

随机推荐