使用 selenium接管浏览器 时遇到的问题

这是代码

#-*- coding: utf-8 -*-
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)
        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)

本来刚开始是能够打开并实现登陆的,但是在后面多试了几次后就不能用了,是因为端口被占用了的原因吗,还是什么原因啊,哪位大佬来帮忙看看,谢谢了 

 

原因找到了

只需将

os.system('chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\Learn\selenium_data"')

改为

os.popen('chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\Learn\selenium_data"')

即可

忽然发现错误提示没有贴完整 补全一下