且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中使用Selenium登录gmail

更新时间:2023-11-21 18:41:28

以下是您的问题的答案:

当我们使用 Python 3.6.1 Selenium 3.4.3 geckodriver v0.17.0 Mozilla Firefox 53.0 一起使用时,我们可以使用定位器 xpath css_selector 通过 Gmail's signin module v2 登录各自的Gmail帐户.>


使用XPATH:

以下是使用xpath登录Gmail的示例代码:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")

driver.get("https://accounts.google.com/signin")
email_phone = driver.find_element_by_xpath("//input[@id='identifierId']")
email_phone.send_keys("your_emailid_phone")
driver.find_element_by_id("identifierNext").click()
password = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, "//input[@name='password']"))
)
password.send_keys("your_password")
driver.find_element_by_id("passwordNext").click()


使用CSS_SELECTOR:

以下是使用css_selector登录Gmail的示例代码:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")

driver.get("https://accounts.google.com/signin")
email_phone = driver.find_element_by_css_selector("#identifierId")
email_phone.send_keys("your_emailid_phone")
driver.find_element_by_css_selector(".ZFr60d.CeoRYc").click()
password = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "input[class='whsOnd zHQkBf'][type='password']"))
)
password.send_keys("your_password")
driver.find_element_by_css_selector(".ZFr60d.CeoRYc").click()

I am trying to log into gmail using Selenium. In the new gmail log in, first you type your email id and then a next page comes where you type your password. URL of email page and password page, both are different. So, when I am passing the password URL in driver.get it is reloading the page and it redirects to email page if you refresh the URL without entering password. Because of this, it is missing the password field selector. current_url is still the previous url, i.e, url of email page. This is my code. I am using chrome driver and python 2.X

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = "/Documents/chromedriver" # Path to chrome-driver
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)

# Email insert 

driver.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin")  #URL of email page
username = driver.find_element_by_id("identifierId")
username.send_keys("myemail")
driver.find_element_by_id("identifierNext").click()


# Password Insert 

driver.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin")  # URL of password page
password = driver.find_element_by_id("password")
password.send_keys("mypassword")
driver.find_element_by_id("passwordNext").click()

#driver.quit()

Here is the Answer to your Question:

When we work with Selenium 3.4.3, geckodriver v0.17.0 and Mozilla Firefox 53.0 using Python 3.6.1, we can use either of the locators xpath or css_selector to log into our respective Gmail accounts through Gmail's signin module v2.


Using XPATH :

Here is the sample code to log into Gmail using xpath:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")

driver.get("https://accounts.google.com/signin")
email_phone = driver.find_element_by_xpath("//input[@id='identifierId']")
email_phone.send_keys("your_emailid_phone")
driver.find_element_by_id("identifierNext").click()
password = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, "//input[@name='password']"))
)
password.send_keys("your_password")
driver.find_element_by_id("passwordNext").click()


Using CSS_SELECTOR:

Here is the sample code to log into Gmail using css_selector:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")

driver.get("https://accounts.google.com/signin")
email_phone = driver.find_element_by_css_selector("#identifierId")
email_phone.send_keys("your_emailid_phone")
driver.find_element_by_css_selector(".ZFr60d.CeoRYc").click()
password = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "input[class='whsOnd zHQkBf'][type='password']"))
)
password.send_keys("your_password")
driver.find_element_by_css_selector(".ZFr60d.CeoRYc").click()