使用 Python Selenium 自动连接我的 Gmail 帐户 [英] Auto connect on my Gmail account with Python Selenium

查看:34
本文介绍了使用 Python Selenium 自动连接我的 Gmail 帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 中的 Selenium 使用我的 GMail 帐户自动登录,但出现此错误:

I am trying to auto-login with my GMail account with Selenium in Python, but I am getting this error:

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

我的代码如下:

#!/usr/bin/python
# coding: utf8

import scrapy

from selenium import webdriver
from scrapy.selector import Selector
from selenium.webdriver.common.action_chains import ActionChains
from scrapy.contrib.spiders import CrawlSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.http import Request, FormRequest
from scrapy import log
from scrapy.exceptions import DropItem
from scrapy import signals
from selenium.webdriver.common.by import By
import scrapy
from scrapy import signals
from scrapy.http import TextResponse 
from scrapy.xlib.pydispatch import dispatcher
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class googleplay(CrawlSpider):
    name = "selenium"
    flag = True

    def __init__(self):
        self.driver = webdriver.Firefox()
        self.driver.get('http://gmail.com')
        action = webdriver.ActionChains(self.driver)
        #User name input field identification and data entered
        usernametext = self.driver.find_element_by_name('Email')
        usernametext.send_keys("myemailaddress@gmail.com") #put your actual username
        self.driver.find_element_by_name('signIn').click()
        time.sleep(2)
        #Password input field identification and data entered
        passwordtext = self.driver.find_element_by_name('Passwd')
        passwordtext.send_keys("password") #put your actual password
        key = self.driver.find_element_by_name('signIn')
        print "----------------------"
        key.click()
        print "----------------------"
        start_urls = ["https://www.mywebsite.com"]    

    def parse(self, reponse):
        #Loading the gmail URL
        print "toto"

我该如何解决我的问题?

How I can fix my problem?

推荐答案

尝试更改 url.使用 https://accounts.google.com/ServiceLogin?service=mail#identifier 而不是 http://gmail.com.

Try changing the url. Use https://accounts.google.com/ServiceLogin?service=mail#identifier instead of http://gmail.com.

确保您使用原始电子邮件和密码.并将每个元素的 self.driver.find_element_by_name 更改为 self.driver.find_element_by_id.

Make sure you use your original email and password. And change self.driver.find_element_by_name to self.driver.find_element_by_id for every element.

您的代码将是:

#!/usr/bin/python
# coding: utf8

import scrapy
import time
from selenium import webdriver
from scrapy.selector import Selector
from selenium.webdriver.common.action_chains import ActionChains
from scrapy.contrib.spiders import CrawlSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.http import Request, FormRequest
from scrapy import log
from scrapy.exceptions import DropItem
from scrapy import signals
from selenium.webdriver.common.by import By
from scrapy import signals
from scrapy.http import TextResponse 
from scrapy.xlib.pydispatch import dispatcher
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class googleplay(CrawlSpider):
    name = "selenium"
    flag = True

    def __init__(self):
        self.driver = webdriver.Firefox()
        self.driver.get('http://gmail.com')
        action = webdriver.ActionChains(self.driver)
        #User name input field identification and data entered
        usernametext = self.driver.find_element_by_name('Email')
        usernametext.send_keys("myemail@gmail.com") #put your actual username
        self.driver.find_element_by_name('signIn').click()
        #Password input field identification and data entered
        passwordtext = self.driver.find_element_by_id('Passwd')
        passwordtext.send_keys("mypassword") #put your actual password
        self.driver.find_element_by_id('signIn').click()
        print "----------------------"
        #key.click()
        print "----------------------"
        start_urls = ["https://www.mywebsite.com"]    

    def parse(self, reponse):
        #Loading the gmail URL
        print "toto" 

这篇关于使用 Python Selenium 自动连接我的 Gmail 帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆