1.1 selenium 介紹
selenium 是一個 web 的自動化測試工具;
1.2 selenium+Python環境配置
1.2.1安裝好Python開發環境(推薦安裝Python3.5及以上版本)
地址:python官網https://www.python.org/
或者命令行安裝python:brew install python3
但是如果想用python的編輯器還需要安裝python有IDEL編輯器
1.2.2 pip 是 Python 包管理工具,該工具提供了對Python 包的查找、下載、安裝、卸載的功能。
目前如果你在 python.org 下載最新版本的安裝包,則是已經自帶了該工具。
Python 2.7.9 + 或 Python 3.4+ 以上版本都自帶 pip 工具。
pip 官網:https://pypi.org/project/pip/
你可以通過以下命令來判斷是否已安裝:
pip3 --version
我的安裝在
如果你還未安裝,則可以使用以下方法來安裝:
pip按照步驟介紹:https://pip.pypa.io/en/latest/installing/
命令:curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py #下載腳本
命令:python get-pip.py #運行腳本
然后執行命令把安裝的pip路徑放在python中:pip 20.0.2 from /Users/july/Library/Python/2.7/lib/python/site-packages/pip (python 2.7)
命令:sudo easy_install pip
1.2.3 安裝selenium
命令窗口切換盤符到pip的scripts目錄執行
python2:pip install selenium
python3:pip3 install selenium
Chrome:我的版本是版本 80.0.3987.149(正式版本) (64 位)
http://chromedriver.storage.googleapis.com/index.html?path=80.0.3987.106/
所以選擇版本 80.0.3987.106對應的mac版本下載
Mac:
復制webdriver到/usr/local/bin目錄下
/usr/local/bin 回車就能看到了
在 chromedriver 的路徑下:
cd /Users/july/Downloads
執行
sudo cp -r chromedriver /usr/local/bin/成功移動。
接着在命令行輸入chromedriver
如果顯示如下,則說明安裝成功。
chengyapingdeMacBook-Pro:Downloads july$ chromedriver
Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
win安裝chromedriver,下載80版本的
python需要配置環境變量:
命令:open .bash_profile #打開環境變量
from selenium import webdriver會不會報錯

像這種不會報錯,證明selenium安裝成功
win測試安裝配置是否完善,核心代碼如下:
>>> from selenium import webdriver
>>> driver=webdriver.Chrome( ) # 創建一個Chrome瀏覽器對象
出現WebDriverException: Message: ‘chromedriver’異常時,下載一個chromedriver,並把它放到python安裝目錄下即可。
如果報錯提示沒有selenium模塊,則需要去selenium官網,下載安裝包
將下載的安裝包放到python的指定路徑site-pachages目錄下,然后進入selenium目錄,執行安裝命令python setup.py install
啟動Chrome瀏覽器:
首先打開IDEL編輯器:command+n新建一個編輯器把代碼寫進去,然后保存任意為主,注意不要把名字命名為selenium,因為selenium模塊需要導入的程序也是selenium.py
from selenium import webdriver
browser = webdriver.Chrome('/Library/Frameworks/Python.framework/Versions/3.8/chromedriver') #路徑是Chromedriver的路徑,任意位置即可
browser.get('https://wx.jk724.com')
如果報錯:
則因為chromedriver沒有配置環境變量,如果把Chromedriver程序放在Chrome安裝目錄中,查看Chrome的安裝目錄在瀏覽器中輸入這個網址:chrome://version/
把Chromediver一起放在macos中即可
然后把Google Chrome配置環境變量
我的執行結果如下:vim ./.bash_profile 確定enter,然后輸入e,載點擊i,然后就打開了bash_profile文件且進入了輸入狀態
source ./.bash_profile,然后使用echo $PATH
(echo+自定義名字(MAVEN_HOME)查看單獨設置的某一條環境變量)就可以看到剛剛添加的路徑了
環境變量配置好,重啟電腦,再次執行程序即可。
Last login: Thu Apr 2 22:19:42 on console
192:~ july$ pip3 install selenium
-bash: pip3: command not found
192:~ july$ pip --version
-bash: pip: command not found
192:~ july$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1764k 100 1764k 0 0 28404 0 0:01:03 0:01:03 --:--:-- 33576
192:~ july$ python get-pip.py
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
Downloading pip-20.0.2-py2.py3-none-any.whl (1.4 MB)
|███████████▏ | 501 kB 3.3 kB/s eta 0:04:45ERROR: Exception:
Traceback (most recent call last):
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/cli/base_command.py", line 186, in _main
status = self.run(options, args)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/commands/install.py", line 331, in run
resolver.resolve(requirement_set)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/legacy_resolve.py", line 177, in resolve
discovered_reqs.extend(self._resolve_one(requirement_set, req))
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/legacy_resolve.py", line 333, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/legacy_resolve.py", line 282, in _get_abstract_dist_for
abstract_dist = self.preparer.prepare_linked_requirement(req)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/operations/prepare.py", line 482, in prepare_linked_requirement
hashes=hashes,
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/operations/prepare.py", line 287, in unpack_url
hashes=hashes,
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/operations/prepare.py", line 159, in unpack_http_url
link, downloader, temp_dir.path, hashes
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/operations/prepare.py", line 303, in _download_http_url
for chunk in download.chunks:
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/utils/ui.py", line 160, in iter
for x in it:
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_internal/network/utils.py", line 39, in response_chunks
decode_content=False,
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_vendor/urllib3/response.py", line 564, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_vendor/urllib3/response.py", line 529, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpxP5i4M/pip.zip/pip/_vendor/urllib3/response.py", line 439, in _error_catcher
raise ReadTimeoutError(self._pool, None, "Read timed out.")
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
192:~ july$ python get-pip.pypip install -U pippip install -U pi
python: can't open file 'get-pip.pypip': [Errno 2] No such file or directory
192:~ july$ pip install -U pip
-bash: pip: command not found
192:~ july$ python get-pip.py
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
Downloading pip-20.0.2-py2.py3-none-any.whl (1.4 MB)
|█████████████████████▋ | 972 kB 2.7 kB/s eta 0:02:53ERROR: Exception:
Traceback (most recent call last):
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/cli/base_command.py", line 186, in _main
status = self.run(options, args)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/commands/install.py", line 331, in run
resolver.resolve(requirement_set)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/legacy_resolve.py", line 177, in resolve
discovered_reqs.extend(self._resolve_one(requirement_set, req))
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/legacy_resolve.py", line 333, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/legacy_resolve.py", line 282, in _get_abstract_dist_for
abstract_dist = self.preparer.prepare_linked_requirement(req)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/operations/prepare.py", line 482, in prepare_linked_requirement
hashes=hashes,
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/operations/prepare.py", line 287, in unpack_url
hashes=hashes,
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/operations/prepare.py", line 159, in unpack_http_url
link, downloader, temp_dir.path, hashes
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/operations/prepare.py", line 303, in _download_http_url
for chunk in download.chunks:
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/utils/ui.py", line 160, in iter
for x in it:
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_internal/network/utils.py", line 39, in response_chunks
decode_content=False,
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_vendor/urllib3/response.py", line 564, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_vendor/urllib3/response.py", line 529, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "/var/folders/m8/73y0wl11063c5_fycvm_85hw0000gn/T/tmpNbmzZ4/pip.zip/pip/_vendor/urllib3/response.py", line 439, in _error_catcher
raise ReadTimeoutError(self._pool, None, "Read timed out.")
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
192:~ july$ python get-pip.py
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
Downloading pip-20.0.2-py2.py3-none-any.whl (1.4 MB)
|████████████████████████████████| 1.4 MB 30 kB/s
Collecting wheel
Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
Installing collected packages: pip, wheel
WARNING: The scripts pip, pip2 and pip2.7 are installed in '/Users/july/Library/Python/2.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script wheel is installed in '/Users/july/Library/Python/2.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.0.2 wheel-0.34.2
192:~ july$ pip3 install selenium
-bash: pip3: command not found
192:~ july$ pip install selenium
-bash: pip: command not found
192:~ july$ pip2 install selenium
-bash: pip2: command not found
192:~ july$ pip --version
-bash: pip: command not found
192:~ july$ pip --version
-bash: pip: command not found
192:~ july$ python2 get-pip.py
-bash: python2: command not found
192:~ july$ python3 get-pip.py
-bash: python3: command not found
192:~ july$ pip install selenium
-bash: pip: command not found
192:~ july$ pip show
-bash: pip: command not found
192:~ july$ pip install pip
-bash: pip: command not found
192:~ july$
192:~ july$ install python-pip
usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]
[-o owner] file1 file2
install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]
[-o owner] file1 ... fileN directory
install -d [-v] [-g group] [-m mode] [-o owner] directory ...
192:~ july$ sudo easy_install pip
Password:
Searching for pip
Best match: pip 20.0.2
Adding pip 20.0.2 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip3.8 script to /usr/local/bin
Installing pip3 script to /usr/local/bin
Using /Users/july/Library/Python/2.7/lib/python/site-packages
Processing dependencies for pip
Finished processing dependencies for pip
192:~ july$ pip show
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
WARNING: ERROR: Please provide a package name or names.
192:~ july$ pip --version
pip 20.0.2 from /Users/july/Library/Python/2.7/lib/python/site-packages/pip (python 2.7)
192:~ july$ pip install selenium
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting selenium
Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
|█████ | 143 kB 2.1 kB/s eta 0:06:03ERROR: Exception:
Traceback (most recent call last):
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/cli/base_command.py", line 186, in _main
status = self.run(options, args)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/commands/install.py", line 331, in run
resolver.resolve(requirement_set)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/legacy_resolve.py", line 177, in resolve
discovered_reqs.extend(self._resolve_one(requirement_set, req))
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/legacy_resolve.py", line 333, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/legacy_resolve.py", line 282, in _get_abstract_dist_for
abstract_dist = self.preparer.prepare_linked_requirement(req)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/operations/prepare.py", line 482, in prepare_linked_requirement
hashes=hashes,
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/operations/prepare.py", line 287, in unpack_url
hashes=hashes,
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/operations/prepare.py", line 159, in unpack_http_url
link, downloader, temp_dir.path, hashes
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/operations/prepare.py", line 303, in _download_http_url
for chunk in download.chunks:
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/utils/ui.py", line 160, in iter
for x in it:
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/network/utils.py", line 39, in response_chunks
decode_content=False,
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_vendor/urllib3/response.py", line 564, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_vendor/urllib3/response.py", line 529, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_vendor/urllib3/response.py", line 439, in _error_catcher
raise ReadTimeoutError(self._pool, None, "Read timed out.")
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
192:~ july$ pip install selenium
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting selenium
Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
|████████████████ | 450 kB 2.1 kB/s eta 0:03:36ERROR: Exception:
Traceback (most recent call last):
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/cli/base_command.py", line 186, in _main
status = self.run(options, args)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/commands/install.py", line 331, in run
resolver.resolve(requirement_set)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/legacy_resolve.py", line 177, in resolve
discovered_reqs.extend(self._resolve_one(requirement_set, req))
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/legacy_resolve.py", line 333, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/legacy_resolve.py", line 282, in _get_abstract_dist_for
abstract_dist = self.preparer.prepare_linked_requirement(req)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/operations/prepare.py", line 482, in prepare_linked_requirement
hashes=hashes,
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/operations/prepare.py", line 287, in unpack_url
hashes=hashes,
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/operations/prepare.py", line 159, in unpack_http_url
link, downloader, temp_dir.path, hashes
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/operations/prepare.py", line 303, in _download_http_url
for chunk in download.chunks:
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/utils/ui.py", line 160, in iter
for x in it:
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_internal/network/utils.py", line 39, in response_chunks
decode_content=False,
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_vendor/urllib3/response.py", line 564, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_vendor/urllib3/response.py", line 529, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/july/Library/Python/2.7/lib/python/site-packages/pip/_vendor/urllib3/response.py", line 439, in _error_catcher
raise ReadTimeoutError(self._pool, None, "Read timed out.")
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
192:~ july$ pip install selenium
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting selenium
Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
|████████████████████████████████| 904 kB 3.9 kB/s
Collecting urllib3
Downloading urllib3-1.25.8-py2.py3-none-any.whl (125 kB)
|████████████████████████████████| 125 kB 3.8 kB/s
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.25.8
192:~ july$