本帖最后由 jklujklu 于 2022-2-17 19:47 编辑
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
# [url=home.php?mod=space&uid=238618]@Time[/url] : 2022/2/17 19:31
# [url=home.php?mod=space&uid=686208]@AuThor[/url] : jklujklu
# [url=home.php?mod=space&uid=621973]@Email[/url] : [url=mailto:jklujklu@126.com]jklujklu@126.com[/url]
# [url=home.php?mod=space&uid=267492]@file[/url] : test.py
# @Software: PyCharm
import _thread
import subprocess
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class Test:
def __init__(self):
self.driver = None
self.chrome_driver = r'd:\chromedriver.exe'
self.chrome_path = 'C:\Program Files\Google\Chrome\Application\chrome.exe'
def open_chrome(self):
subprocess.run([self.chrome_path, '--remote-debugging-port=9222'])
def init_driver(self):
_thread.start_new_thread(self.open_chrome, ())
time.sleep(3)
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=chrome_options)
def run(self):
self.init_driver()
self.driver.get("https://www.bilibili.com/")
if __name__ == '__main__':
Test().run()
我这边测试是可以链接的,步骤是:
1. cmd打开chrome
2. selenium连接调试端口
如果带调试的chrome已经打开了,就把代码里thread线程注释掉,每次运行确保只存在一个chrome |