浏览器版本133
旧版 12和11
调试启动代码
import subprocess
cmd = 'chrome.exe --remote-debugging-port=8234 --user-data-dir="C:\\Users\\BigFish\\AppData\\Local\\Google\\Chrome\\User Data"'
subprocess.run(cmd)
selenium连接代码
import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
class Spider():
def __init__(self):
self.service=Service(executable_path=driver_path)
self.chrome_options = ChromeOptions()
self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:8234")
self.driver = webdriver.Chrome(options=self.chrome_options,service=self.service)
self.driver.implicitly_wait(10)
self.item_list=[]
def run(self):
self.driver.get(url)
time.sleep(3)
print(self.driver.page_source)
driver_path = r'C:\Users\BigFish\AppData\Local\Google\Chrome\Application\chromedriver.exe'
url='https://www.baidu.com'
item_list=[]
if __name__ == '__main__':
spider=Spider()
spider.run()
问题1:新版浏览器selenium只能链接唯一一个初始化标签页,其他标签页无法连接,初始化标签页关闭后就无法用selnium连接。旧版可以自动切换标签页

问题2:新版浏览器调试启动后只能通过selenium连接一次,再次执行代码就会返回下面的html ,浏览器也没有进行请求。旧版不会这样,会正常返回请求页面
<html><head>
<meta charset="utf-8">
</head>
<body>
<script src="background.js" type="text/javascript"></script>
</body></html>