Automating GMAIL login using Python-Selenium
You are trying to find the Passwd
id of the element which is not loaded in dom yet. Try adding some delay so that the page could load.
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()
recommended method is browser.implicitly_wait(num_of_seconds)
see this
In 2020 signing in with Gmail is much more tougher because Gmail takes selenium operated window as a bot window and will give a message like this one.img of error
But now I have found a successful way to log in without any error or warning by google.
you can log in to another website as google will accept it as secure login using another website like StackOverflow or some other website with Gmail login.
def gmail_sign_in(email, password): driver = webdriver.Chrome() driver.get('https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent') driver.find_element_by_xpath('//*[@id="openid-buttons"]/button[1]').click() driver.find_element_by_xpath('//*[@id="identifierId"]').send_keys(email) input = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="identifierNext"]/span/span')) ) input.click() driver.implicitly_wait(1) driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys(password) input = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="passwordNext"]/span/span')) ) input.click() driver.implicitly_wait(1) driver.get('https://www.google.com/')
hope the code is understandable just this function and put your email and password make sure you have the appropriate imports.
from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC
I hope that, it will be helpful for automate the gmail in updated chrome version.
from selenium import webdriverimport timedriver = webdriver.Chrome()driver.get("http://gmail.com")driver.find_element_by_id("identifierId").send_keys('your mail id')driver.find_element_by_id("identifierNext").click()time.sleep(5)driver.find_element_by_name("password").send_keys('your password')driver.find_element_by_id("passwordNext").click()time.sleep(5)driver.get("https://accounts.google.com/SignOutOptions?hl=en&continue=https://mail.google.com/mail&service=mail")driver.find_element_by_xpath('//button[normalize-space()="Sign out"]').click()driver.close()