我想将Selenium chrome Web驱动程序启动到当前现有会话。
因此,我尝试通过远程调试进行连接,而不是启动新实例。
所以我所做的是由cmd在127.0.0.1:6813启动了chrome。
C:\Program Files (x86)\Google\Chrome\Application>chrome.exe '127.0.0.1:6813'
一个新的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:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.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:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 245, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.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()
答案 0 :(得分:0)
我知道这个答案可能不再对您有用,但是对于仍然遭受此问题困扰的人,我已经找到了解决方案。 首先,当您尝试创建chrome实例时,还需要为该实例创建一个新的配置文件。 这是一个命令: 为了运行此命令,您将必须在系统路径中添加chrome或转到chrome的安装目录,对于Windows,通常是以下路径:C:\ Program Files(x86)\ Google \ Chrome \ Application >
@Saga
现在,您应该在此实例中运行chrome实例,您可以打开所需的任何内容,一旦完成,就可以使用下面的python代码与此chrome实例进行连接。
chrome.exe -remote-debugging-port=9014 --user-data-dir="<AnyDirectoryOfYourChoice>"
连接成功后,驱动程序将引用您的chrome实例,您可以像通常在硒中那样进行控制。