繁体   English   中英

selenium4 无法连接到本地主机上的 chrome

[英]selenium4 can't connect to chrome on localhost

我要疯了,试图在 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/')

我想我尝试了所有可能的选择:

  • 将 http:// 添加到 localhost 会引发错误,
  • 使用 chrome_options=chrome_options 会引发弃用警告,指出我应该使用 >options=<,并且似乎对 localhost 中的浏览器没有影响,
  • 尝试从本地文件启动 webdriver,目前建议使用 ChromeDriverManager。 两者都有效。 直到我想指定选项。

我还浏览了我在大约 20 个网站上找到的示例,包括。 github 错误报告 - 人们声称他们的代码在 VSCode 升级之前可以正常工作。

我想我的问题是 - 我尝试传递 chrome 选项的方式有什么问题,还是我真的遇到了错误?

====== 编辑:这是一个完整的错误跟踪:

---------------------------------------------------------------------------
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: 

好吧,您没有导入 webdriver,并且您已将 Service 命名为 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

然后避免通过Options传递端口并使用Service的选项端口(现在称为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/')

我终于想通了,尽管我不得不说这比知道如何更幸运。 文档非常缺乏:(

如果有人正在寻找如何控制已经运行的浏览器,特别是使用 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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM