一尘不染

在Python中使用Selenium登录gmail

selenium

我正在尝试使用Selenium登录gmail。在新的gmail登录中,首先输入您的电子邮件ID,然后在下一页输入密码。电子邮件页面的URL和密码页面的URL不同。因此,当我在其中传递密码URL时,driver.get它将重新加载页面,并且如果您刷新URL而未输入密码,它将重定向到电子邮件页面。因此,它缺少密码字段选择器。current_url仍然是先前的网址,即电子邮件页面的网址。这是我的代码。我正在使用Chrome驱动程序和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()

阅读 723

收藏
2020-06-26

共1个答案

一尘不染

这是您问题的答案:

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


使用XPATH:

这是使用来登录Gmail的示例代码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()

使用CSS_SELECTOR:

这是使用来登录Gmail的示例代码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()
2020-06-26