簡體   English   中英

Gmail 中的錯誤通過谷歌搜索通過 Chromedriver 使用 Selenium Python 自動登錄

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

我是 RPA 新手,試圖通過使用 Python Selenium-

我的代碼-

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

但在最后一行,它得到了一個錯誤 -

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

為什么顯示此錯誤?

注意-我正在使用 chrome 驅動器 83.0.4103.39 完整錯誤的圖片-

在此處輸入圖像描述

你能幫我解決這個錯誤嗎?我沒有通過谷歌搜索得到任何幫助。 提前致謝。

我有另一種解決方案,可以毫不費力地工作。 未檢測到的瀏覽器 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()

除此之外,selenium 線還有許多很棒的功能,請查看 Github存儲庫

顯示錯誤是因為帳戶登錄頁面無法從上一頁獲取句柄,因此無法檢測當前頁面上的 Xpath,這就是顯示錯誤的原因。

解決方案-

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