![](/img/trans.png)
[英]selenium.common.exceptions.WebDriverException: Message: target frame detached error using Selenium and Chromedriver via 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.