我想在本地主机上连接Selenium4和Chrome实例。我从bash调用chrome
$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=6666 --user-data-dir=/Users/h/Desktop/MY/chrome-profile接下来我尝试从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/')我想我试过了所有可能的选择:
我还浏览了我在大约20个网站上找到的例子,包括。github报告-在那里人们声称他们的代码工作直到VSCode升级。
我想我的问题是-我如何试图传递铬选项有什么问题,或者我真的遇到了一个错误?
======编辑:这是一个完整的错误跟踪:
---------------------------------------------------------------------------
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: 发布于 2022-09-12 23:16:42
好吧,您不是在导入webdriver,而是将服务命名为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然后避免传递带有选项的端口,并使用选项服务端口(现在称为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/')发布于 2022-09-13 19:30:56
我终于想出了答案,尽管我不得不说,这更多的是运气,而不是知道如何。文件非常缺乏:(
如果有人在寻找如何控制已经在运行的浏览器,特别是使用selenium4和python,请看以下内容:
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')https://stackoverflow.com/questions/73695925
复制相似问题