1

I am new to RPA trying to do Gmail login automation by searching the Gmail in the google search(Searching will also be automated) using Python Selenium-

My code-

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

#Code for lauching google chrome browser
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('- headless')
#chrome_options.add_argument('- no-sandbox')
#chrome_options.add_argument('- disable-dev-shm-usage')

driver = webdriver.Chrome("F:\\RPA\\Using Python\\chromedriver.exe", chrome_options = chrome_options)

driver.get("https://WWW.google.com/")
#print(driver.page_source)

xpathsearch = "//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input"
searchinput = driver.find_element_by_xpath(xpathsearch)

searchinput.send_keys("Gmail")
searchinput.send_keys(Keys.ENTER)


xpathresult = "//*[@id='rso']/div[1]/div/div[1]/a/h3"
driver.find_element_by_xpath(xpathresult).click()

xpathresult = "/html/body/div[2]/div[1]/div[4]/ul[1]/li[2]/a"
driver.find_element_by_xpath(xpathresult).click(  )

xpathresult = "//*[@id='identifierId']"
driver.find_element_by_xpath(xpathresult).send_keys("[email protected]")

But in the last line, it got an error-

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"xpathresult"}
  (Session info: chrome=83.0.4103.116)

why this error is showing?

N.B. - I am using chrome drive 83.0.4103.39 The pic of the full error-

enter image description here

Can you help me out to solve this error I didn't get any help by googling. Thanks in advance.

5
  • What's the entire error? Usually it gives a line number Commented Jul 14, 2020 at 9:39
  • Let me post the screenshot of the error then Commented Jul 14, 2020 at 9:42
  • 1
    Things you need to keep in mind while using selenium, add some delays for 2-3 secs (just waiting for gmail to load completely) also be assured that there is such element with xpath present (i hope you have not copy pasted the code from some resource because xpath is relative(changes)). Commented Jul 14, 2020 at 9:54
  • the xpath is correct. I didn't copy & paste the code I took the path directly from the chrome developer tab. Commented Jul 14, 2020 at 10:05
  • I solved it. I am putting the solution into the answer Commented Jul 15, 2020 at 4:17

2 Answers 2

1

I have another solution that works without any hustle. Use Seleniumwire with undetected browser v2

from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
import time

options = {}
chrome_options = ChromeOptions()
chrome_options.add_argument('--user-data-dir=hash')
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-dev-shm-usage")
# chrome_options.add_argument("--headless")
browser = Chrome(seleniumwire_options=options, options=chrome_options)

browser.get('https://gmail.com')
browser.find_element_by_xpath('//*[@id="identifierId"]').send_keys('your-email')
browser.find_element_by_xpath('//*[@id="identifierNext"]/div/button').click()
time.sleep(5)
browser.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys('you-password')
browser.find_element_by_xpath('//*[@id="passwordNext"]/div/button').click()

In addition to this, selenium wire has many awesome features, check out Github repository

Sign up to request clarification or add additional context in comments.

Comments

0

The error is showing because the account login page can't get the handle from the previous page so it can't detect the Xpath on the current page that's why the error is showing.

Solution-

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

#Code for lauching google chrome browser
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('- headless')
#chrome_options.add_argument('- no-sandbox')
#chrome_options.add_argument('- disable-dev-shm-usage')

driver = webdriver.Chrome("F:\\RPA\\Using Python\\chromedriver.exe", chrome_options = chrome_options)

driver.get("https://WWW.google.com/")
#print(driver.page_source)

xpathsearch = "//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input"
searchinput = driver.find_element_by_xpath(xpathsearch)

searchinput.send_keys("Gmail")
searchinput.send_keys(Keys.ENTER)


xpathresult = "//*[@id='rso']/div[1]/div/div[1]/a/h3"
driver.find_element_by_xpath(xpathresult).click()

xpathresult = "/html/body/div[2]/div[1]/div[4]/ul[1]/li[2]/a"
driver.find_element_by_xpath(xpathresult).click()

pre_login=driver.current_window_handle

for handle in driver.window_handles:
    if not(handle==pre_login):
        print(handle)
        login_page = handle
        break
        
driver.switch_to_window(login_page)
        

driver.find_element_by_class_name("Aa1VU")

xpathresult = '//*[@id="identifierId"]'
driver.find_element_by_xpath(xpathresult).send_keys("[email protected]")
driver.find_element_by_xpath('//*[@id="identifierNext"]/div/button/div[2]').click() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.