本文介绍了如何使用Chrome驱动中的DEBUGER_ADDRESS选项进行远程调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要将我的Selify Chrome Web驱动程序启动到当前现有会话。
所以我尝试通过远程调试进行连接,而不是启动新实例。
所以我所做的是cmd在127.0.0.1:6813启动Chrome。
C:Program Files (x86)GoogleChromeApplication>chrome.exe '127.0.0.1:6813'
已启动一个新的Chrome实例。
现在,我在代码中使用Chrome选项添加了DEBUGGER_ADDRESS="127.0.0.1:6813"。
chrome_options.debugger_address="127.0.0.1:6813"
但我的Spyder IDE出现以下错误:
runfile('C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi/temp7.py', wdir='C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi')
Traceback (most recent call last):
File "<ipython-input-9-77e28441ef9f>", line 1, in <module>
runfile('C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi/temp7.py', wdir='C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi')
File "C:ProgramDataAnaconda3libsite-packagesspyderutilssitesitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:ProgramDataAnaconda3libsite-packagesspyderutilssitesitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi/temp7.py", line 22, in <module>
browser = webdriver.Chrome(chrome_options=chrome_options,executable_path=cpath)
File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriverchromewebdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriver
emotewebdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriver
emotewebdriver.py", line 245, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriver
emotewebdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriver
emoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: unknown error: cannot connect to chrome at 127.0.0.1:6813
from chrome not reachable
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.17134 x86_64)
完整代码:
import pyautogui
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException,TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
fpath="C:/Users/Gupta Niwas/Downloads/Softwares/Browsers/Drivers/geckodriver-v0.21.0-win64/geckodriver.exe"
cpath="C:/Users/Gupta Niwas/Downloads/Softwares/Browsers/Drivers/chromedriver_win32/chromedriver.exe"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("start-maximized")
chrome_options.debugger_address="127.0.0.1:6813"
#browser = webdriver.Firefox(executable_path=fpath)
browser = webdriver.Chrome(chrome_options=chrome_options,executable_path=cpath)
#browser.execute_async_script("function(){(function(a){document.body.appendChild(a);a.setAttribute('href', 'http://google.com');a.dispatchEvent((function(e){e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);return e;}(document.createEvent('MouseEvents'))))}(document.createElement('a')));}")
browser.get('https://youngeinsteins.com/')
pyautogui.hotkey('ctrl','t')
WebDriverWait(browser,100).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'html.gtie9.js.flexbox.canvas.canvastext.webgl.no-touch.geolocation.postmessage.no-websqldatabase.indexeddb.hashchange.history.draganddrop.websockets.rgba.hsla.multiplebgs.backgroundsize.borderimage.borderradius.boxshadow.textshadow.opacity.cssanimations.csscolumns.cssgradients.no-cssreflections.csstransforms.csstransforms3d.csstransitions.fontface.generatedcontent.video.audio.localstorage.sessionstorage.webworkers.applicationcache.svg.inlinesvg.smil.svgclippaths.dk_fouc body.home.page-template-default.page.page-id-15 div.subscribe-popup-wrap div.subscribe-popup div.close svg.icon-cross polygon')))
cross=browser.find_element_by_css_selector('html.gtie9.js.flexbox.canvas.canvastext.webgl.no-touch.geolocation.postmessage.no-websqldatabase.indexeddb.hashchange.history.draganddrop.websockets.rgba.hsla.multiplebgs.backgroundsize.borderimage.borderradius.boxshadow.textshadow.opacity.cssanimations.csscolumns.cssgradients.no-cssreflections.csstransforms.csstransforms3d.csstransitions.fontface.generatedcontent.video.audio.localstorage.sessionstorage.webworkers.applicationcache.svg.inlinesvg.smil.svgclippaths.dk_fouc body.home.page-template-default.page.page-id-15 div.subscribe-popup-wrap div.subscribe-popup div.close svg.icon-cross polygon')
cross.click()
推荐答案
我知道此答案可能对您不再有用,但对于那些仍在遭受此问题困扰的人,我找到了一个解决方案。首先,当您尝试创建一个Chrome实例时,还需要为该实例创建一个新的配置文件。以下是实现这一点的命令:要运行此命令,您必须将Chrome添加到系统路径或转到安装Chrome的目录,对于Windows,通常是以下路径:C:Program Files(X86)GoogleChromeApplication
chrome.exe -remote-debugging-port=9014 --user-data-dir="<AnyDirectoryOfYourChoice>"
现在,您应该有了一个在此实例中运行的Chrome实例,您可以打开任何您想要的东西,并且一旦完成,您就可以使用下面的Python代码来连接此Chrome实例。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9014")
driver = webdriver.Chrome(options=chrome_options)
连接成功后,驱动程序将拥有对您的Chrome实例的引用,您可以像我们在Selify中通常所做的那样进行控制。
这篇关于如何使用Chrome驱动中的DEBUGER_ADDRESS选项进行远程调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!