关于使用selenium接管浏览器时遇到的问题
代码:
import os
import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class ZhiHu:
def init(self):
self.url = 'https://www.zhihu.com/'
self.chrome_options = Options()
self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # 前面设置的端口号
self.browser = webdriver.Chrome(executable_path='../chromedriver.exe', options=self.chrome_options) # executable执行webdriver驱动的文件
def get_start(self):
self.browser.get(self.url)
#time.sleep(20) # 可以选择手动登录或者是自动化,我这里登录过就直接登陆了
info = self.browser.get_cookies() # 获取cookies
print(info)
with open(r"info.json", 'w', encoding='utf-8') as f:
f.write(json.dumps(info))
self.browser.close()
if name == 'main':
os.system('chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\Learn\selenium_data"')
zhihu = ZhiHu()
zhihu.get_start()
提示的错误:
[11768:5712:0330/110048.363:ERROR:external_registry_loader_win.cc(160)] File D:\杞欢\Xdown\xdown\eapmjcdkdlenhkbanlgacimfibbbiinc.crx for key Software\Google\Chrome\Extensions\eapmjcdkdlenhkbanlgacimfibbbiinc does not exist or is not readable.
DevTools listening on ws://127.0.0.1:9222/devtools/browser/f6af029e-e644-4404-a872-4e992da7b162
[11768:9968:0330/110052.869:ERROR:device_event_log_impl.cc(214)] [11:00:52.868] USB: usb_device_handle_win.cc:1056 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)
[11768:9968:0330/110052.870:ERROR:device_event_log_impl.cc(214)] [11:00:52.869] USB: usb_device_handle_win.cc:1056 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:9222
from chrome not reachable
回答
https://www.cnblogs.com/pythonywy/p/13805061.html,你都没打开指定端口浏览器还指望控制指定浏览器