Reputation: 526
I want to launch my selenium chrome web driver to current existing session.
So I am trying to connect via remote debugging instead of starting a new instance.
So what I did is launched chrome at 127.0.0.1:6813 by cmd.
C:\Program Files (x86)\Google\Chrome\Application>chrome.exe '127.0.0.1:6813'
A new instance of chrome started.
Now in my code I added debugger_address="127.0.0.1:6813" using chrome options.
chrome_options.debugger_address="127.0.0.1:6813"
But my Spyder IDE gives following error:
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)
Full code:
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()
Upvotes: 7
Views: 26616
Reputation: 193
This worked for me:
chrome_options.add_argument('--remote-debugging-port=9222')
Upvotes: 6
Reputation: 11
There seems to be a way to also start a remote debugging server with firefox.
firefox.exe --start-debugger-server 9224 -profile C:\<profilepath>
but since theadd_experimental_option()
-method is sole available to the "Chromeoptions" class it might not be an easy,direct conversion to firefox.
Upvotes: 1
Reputation: 259
I know this answer may not be useful to you anymore but for those who are still suffering from this problem, I have found a solution. First of all, when you try to create a chrome instance you need to create a new profile as well, for that instance. here is a command for that: In order to run this command, you will have to add chrome to system path or go to the directory where chrome is installed, for windows it is usually this path: C:\Program Files (x86)\Google\Chrome\Application
chrome.exe -remote-debugging-port=9014 --user-data-dir="<AnyDirectoryOfYourChoice>"
Now you should have an instance of chrome running in this instance you can open anything you want and once you are done, you can use this python code below to connect with this instance of 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)
Once you have connected successfully driver will have the reference to your chrome instance and you can control as we normally do in selenium.
Upvotes: 17