简体   繁体   English

Gmail 中的错误通过谷歌搜索通过 Chromedriver 使用 Selenium Python 自动登录

[英]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-我是 RPA 新手,试图通过使用 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-注意-我正在使用 chrome 驱动器 83.0.4103.39 完整错误的图片-

在此处输入图像描述

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未检测到的浏览器 v2中使用Seleniumwire

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除此之外,selenium 线还有许多很棒的功能,请查看 Github存储库

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.显示错误是因为帐户登录页面无法从上一页获取句柄,因此无法检测当前页面上的 Xpath,这就是显示错误的原因。

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() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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