Selenium4.6版本瀏覽器自動(dòng)退出問題
代碼
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com') # 這一步其實(shí)沒必要,只是為了演示
現(xiàn)象
-
打開百度后chrome瀏覽器就關(guān)閉掉了
-
代碼的控制臺并無任何報(bào)錯(cuò)
-
正常應(yīng)該保留的
環(huán)境
- python 3.10.8
- pycharm 2021.2.3
- chrome 107.0.5304.122
- chromedriver 107.0.5304.18
- selenium 4.6.1
分析
- 這個(gè)問題在之前的版本中幾乎沒遇到過,這一期的學(xué)員很多都反饋有該問題,推測應(yīng)該是新版本導(dǎo)致的
- python 沒有變化,排除
- pycharm 更沒有變化(脫離pycharm運(yùn)行一樣報(bào)錯(cuò),排除)
- chrome 更新了
- chromedriver 更新了
- selenium 更新了
嘗試1: 分析日志
-
獲取selenium的日志
-
代碼
from selenium import webdriver driver = webdriver.Chrome(service_args=['--verbose'],service_log_path='selenium.log') driver.get('https://www.baidu.com')
-
差異日志: 對比正常的環(huán)境有2行比較異常
[1669339280.964][INFO]: [9a850cc416a680214e963aab4064f86b] COMMAND QuitAll {} [1669339281.111][INFO]: [9a850cc416a680214e963aab4064f86b] RESPONSE QuitAll
-
不得法
嘗試2: stackoverflow
https://stackoverflow.com/questions/74567988/browser-quit-automatically-by-using-selenium-on-chrome
-
提交到stackoverflow上,有人建議
from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options options = Options() options.add_experimental_option("detach", True) driver = webdriver.Chrome(service=Service(<chromedriver.exe path>), options=options)
-
這是實(shí)驗(yàn)性質(zhì)的option
-
融合到代碼中
from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium import webdriver options = Options() options.add_experimental_option("detach", True) driver = webdriver.Chrome(options=options) driver.get('https://www.baidu.com')
-
效果是ok的
-
但以前是ok的,現(xiàn)在這么麻煩,對初學(xué)者并不友好
嘗試3: 對比分析
-
對比之前好的環(huán)境,發(fā)現(xiàn)差異主要是瀏覽器、驅(qū)動(dòng)和selenium,把瀏覽器和驅(qū)動(dòng)都同步到最新版本,環(huán)境依然是好的,那差異只有selenium版本(有問題的是selenium 4.6.1,好的版本是selenium4.3.0)
-
降級版本: 如果你是虛擬環(huán)境,請注意執(zhí)行位置
pip uninstall selenium pip instsall selenium==4.3.0
-
再次運(yùn)行代碼,也OK了。文章來源:http://www.zghlxwxcb.cn/news/detail-787655.html
-
感覺是selenium4.6配合chromedriver驅(qū)動(dòng)出現(xiàn)的問題,firefox在selenium4.6的時(shí)候沒有該問題。文章來源地址http://www.zghlxwxcb.cn/news/detail-787655.html
到了這里,關(guān)于Selenium4.6版本瀏覽器自動(dòng)退出問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!