简体   繁体   中英

Error in Gmail Login by google searching Automation via Chromedriver using Selenium Python

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("somename2651996@gmail.com")

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?

NB - I am using chrome drive 83.0.4103.39 The pic of the full error-

在此处输入图像描述

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

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

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("somemailid@gmail.com")
driver.find_element_by_xpath('//*[@id="identifierNext"]/div/button/div[2]').click() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM