I'm going nuts trying to connnect in Selenium4 to Chrome instance on a localhost. I invoked chrome from bash using
$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=6666 --user-data-dir=/Users/h/Desktop/MY/chrome-profile
and next i tried to connect from Selenium4
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
# opt = Options()
# opt.add_experimental_option("debuggerAddress", "localhost:6666")
# opt.add_argument("debuggerAddress", "localhost:6666")
# opt.add_debuggerAddress("localhost:6666")
# opt.add_debugger_address("localhost:6666")
# web_driver = webdriver.Chrome(options=opt)
# chrome_options = Options()
# chrome_options.add_experimental_option("debuggerAddress", "localhost:6666")
# driver = webdriver.Chrome(chrome_options=chrome_options)
# driver.get('https://www.google.com')
path = '/Users/h/Desktop/MY/webdrivers/chrome/105.0.5195.52/chromedriver'
# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=opt)
# service = Service(executable_path=ChromeDriverManager().install())
# driver = webdriver.Chrome(service=service, options=chrome_options)
# driver.get('https://www.example.com/')
service = ChromeService(executable_path=path, port=6666) # i tried with local path and ChromeDriverManager
driver = webdriver.Chrome(service=service)
driver.get('https://www.example.com/')
i think i tried all possible options:
- prepending http:// to localhost throws an error,
- using chrome_options=chrome_options throws a deprecation warning, pointing i should use >options=<, and seems to have no effect on the browser in localhost,
- trying to launch webdriver from a local file, and currently suggested ChromeDriverManager. Both work. Until i want to specify options.
i also looped through the example i found on ~20 websites, incl. github bug reports - where people claim their code worked until VSCode upgraded.
I guess my question is - is there anything wrong with how i try to pass the chrome options, or did i actually encounter a bug ?
====== Edit: that is a full error trace:
---------------------------------------------------------------------------
WebDriverException Traceback (most recent call last)
Input In [10], in <cell line: 29>()
22 # driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=opt)
23
24 # service = Service(executable_path=ChromeDriverManager().install())
25 # driver = webdriver.Chrome(service=service, options=chrome_options)
26 # driver.get('https://www.wp.pl/')
28 service = ChromeService(executable_path=path, port=6666) #at this line you can use your ChromeDriverManager
---> 29 driver = webdriver.Chrome(service=service)
30 driver.get('https://www.example.com/')
File ~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py:69, in WebDriver.__init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, service, keep_alive)
66 if not service:
67 service = Service(executable_path, port, service_args, service_log_path)
---> 69 super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
70 port, options,
71 service_args, desired_capabilities,
72 service_log_path, service, keep_alive)
File ~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py:92, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, port, options, service_args, desired_capabilities, service_log_path, service, keep_alive)
89 self.service.start()
91 try:
---> 92 super().__init__(
93 command_executor=ChromiumRemoteConnection(
94 remote_server_addr=self.service.service_url,
95 browser_name=browser_name, vendor_prefix=vendor_prefix,
96 keep_alive=keep_alive, ignore_proxy=_ignore_proxy),
97 options=options)
98 except Exception:
99 self.quit()
File ~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py:270, in WebDriver.__init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
268 self._authenticator_id = None
269 self.start_client()
--> 270 self.start_session(capabilities, browser_profile)
File ~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py:363, in WebDriver.start_session(self, capabilities, browser_profile)
361 w3c_caps = _make_w3c_caps(capabilities)
362 parameters = {"capabilities": w3c_caps}
--> 363 response = self.execute(Command.NEW_SESSION, parameters)
364 if 'sessionId' not in response:
365 response = response['value']
File ~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py:428, in WebDriver.execute(self, driver_command, params)
426 response = self.command_executor.execute(driver_command, params)
427 if response:
--> 428 self.error_handler.check_response(response)
429 response['value'] = self._unwrap_value(
430 response.get('value', None))
431 return response
File ~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py:207, in ErrorHandler.check_response(self, response)
205 value = response['value']
206 if isinstance(value, str):
--> 207 raise exception_class(value)
208 if message == "" and 'message' in value:
209 message = value['message']
WebDriverException: Message:
CodePudding user response:
well, you are not making import of webdriver, and you have named Service as ChromeService.
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium import webdriver
Then avoid to pass the port with Options and use the option port of Service (now called ChromeServie)
# chrome_options = Options()
# chrome_options.add_experimental_option("debuggerAddress", "localhost:6666")
service = ChromeService(executable_path="chromedriver", port=6666) #at this line you can use your ChromeDriverManager
driver = webdriver.Chrome(service=service)
driver.get('https://www.example.com/')
CodePudding user response:
I finally figured it out, though i have to say it was more luck than knowing how to. The documentation is super lacking :(
If anyone is looking for how to take control of already running browser, specifically with selenium4 and python, here goes:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("debuggerAddress", "localhost:6666")
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options = chrome_options)
driver.get('http://example.com')