最近在學著使用blackwidow這個工具,在ubuntu20.02系統的安裝過程當中遇到了selenium使用的一些問題。 selenium是個什麼工具? Selenium是一個用於Web應用程式測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。支持的瀏覽器包括IE(7 ...
最近在學著使用blackwidow這個工具,在ubuntu20.02系統的安裝過程當中遇到了selenium使用的一些問題。
selenium是個什麼工具?
Selenium是一個用於Web應用程式測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。支持的瀏覽器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera,Edge等。這個工具的主要功能包括:測試與瀏覽器的相容性——測試應用程式看是否能夠很好得工作在不同瀏覽器和操作系統之上。測試系統功能——創建回歸測試檢驗軟體功能和用戶需求。支持自動錄製動作和自動生成.Net、Java、Perl等不同語言的測試腳本
這一個工具同時也集成到了python的庫當中,通過引用模塊就可以使用。
不過如果遇到了以下這種錯誤:
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
crawl.py:29: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(chrome_options = chrome_options)
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "crawl.py", line 29, in <module>
driver = webdriver.Chrome(chrome_options = chrome_options)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 89, in __init__
self.service.start()
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
Selenium3.x調用瀏覽器必須有一個webdriver驅動文件,錯誤是no such file or directory: "chromedriver"
的意思就是電腦沒有安裝chrome,也沒有找到軟體路徑。需要先去官網上下載一下這個壓縮包
parallels@parallels-Parallels-Virtual-Platform:~/BlackWidow$ sudo wget https://chromedriver.storage.googleapis.com/107.0.5304.18/chromedriver_linux64.zip
--2022-10-13 18:57:21-- https://chromedriver.storage.googleapis.com/107.0.5304.18/chromedriver_linux64.zip
Resolving chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)... 142.250.204.48
Connecting to chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)|142.250.204.48|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6876869 (6.6M) [application/zip]
Saving to: ‘chromedriver_linux64.zip’
chromedriver_linux64.zip 100%[====================================================================================================>] 6.56M 5.34MB/s in 1.2s
2022-10-13 18:57:22 (5.34 MB/s) - ‘chromedriver_linux64.zip’ saved [6876869/6876869]
安裝之後進行解壓,然後就直接得到一個可執行文件,我們再繼續運行原來的命令,又會遇到新的以下的問題
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
crawl.py:29: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(chrome_options = chrome_options)
Traceback (most recent call last):
File "crawl.py", line 29, in <module>
driver = webdriver.Chrome(chrome_options = chrome_options)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
super().__init__(
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 272, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 364, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
找不到chrome binary,這種情況一般是瀏覽器對應的驅動沒有再預設路徑下找到瀏覽器的二進位文件。
首先我們檢查到主機上就沒有安裝谷歌瀏覽器
parallels@parallels-Parallels-Virtual-Platform:~/BlackWidow$ chromium-browser -version
Command 'chromium-browser' not found, but can be installed with:
sudo apt install chromium-browser
1. parallels@parallels-Parallels-Virtual-Platform:~/BlackWidow$ sudo apt install chromium-browser
2. 將之前下載的文件移動到該路徑/usr/bin,命令: sudo mv chromedriver /usr/bin
3. 切換到/usr/bin目錄,命令: cd /usr/bin
4. 使得其變成可執行文件,命令 :sudo chmod a+x chromedriver
最後再執行工具命令就可以了
本文來自博客園,作者:ivanlee717,轉載請註明原文鏈接:https://www.cnblogs.com/ivanlee717/p/16789389.html