Python實現網頁自動化


之前寫過一篇  使用selenium控制(接管)已打開的瀏覽器(chrome),並通過WebDriver值檢測

1.安裝selenium庫

pip install selenium

2.到 http://npm.taobao.org/mirrors/chromedriver/下載 chromedriver

我的chrome是版本 97.0.4668.0(正式版本)canary (64 位),該網頁最新的只有95.0.4638.17/   ,就用這個試試吧
下載win32版本,解壓出  chromedriver.exe  放在 test.py所在目錄

3.test.py

from selenium import webdriver

#設置自動化打開的瀏覽器訪問網址
url = 'https://www.baidu.com'

#設置谷歌瀏覽器driver的目錄所在
path = '.'

browser = webdriver.Chrome(executable_path=path)

#打開瀏覽器,並訪問設置的網址。
browser.get(url)

運行,提示:PermissionError: [WinError 5] 拒絕訪問。

4 通過向python310文件夾或chromedriver.exer文件的 屬性 賦予 Users組完全控制權限   也未能解決

pip install --user --upgrade selenium 也不行。

最后發現是上面代碼中path路徑不對,將

path = '.' 改為
path = './chromedriver.exe'

這回提示:

This version of ChromeDriver only supports Chrome version 95
Current browser version is 97.0.4668.0 with binary path C:\Users\你的用戶名\AppData\Local\Google\Chrome SxS\Application\chrome.exe

5 看樣子要降級chrome至95版本

5.1   訪問 chrome://version/  記下個人資料路徑

5.2 利用win10自帶功能卸載chrome(同時刪除個人資料)

5.3刪除個人資料路徑 C:\Users\cc\AppData\Local\Google\Chrome SxS\User Data\Default\Web Data文件

5.4 到https://chromium.cypress.io/ 找到95.0.4638.17 Windows (x64)版本chrome ,可惜被牆了下不了,於是百度搜索從系統之家官網下了一個  ,安裝后,顯示無法再更新了,先用着吧。

 重新運行腳本test.py,可以了。

總結:運行以下代碼似乎通過了測試

from selenium import webdriver
from selenium.webdriver.chrome.options import Options 
chrome_options = Options()
chrome_driver = "chromedriver.exe" 
driver = webdriver.Chrome(chrome_driver, options=chrome_options)
script = '''
Object.defineProperty(navigator, 'webdriver', {
    get: () => undefined
})
'''
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": script})
print(driver.title)
driver.get("https://intoli.com/blog/not-possible-to-block-chrome-headless/chrome-headless-test.html")

 

 

參考:https://www.cnblogs.com/chen1999/p/12906921.html

https://blog.csdn.net/weixin_43870646/article/details/90020874

https://www.cnpython.com/qa/103559

https://blog.csdn.net/iphone108/article/details/103138026


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM