selenium+python自动化

1.1 selenium 介绍

selenium 是一个 web 的自动化测试工具;

1.2 selenium+Python环境配置

1.2.1安装好Python开发环境(推荐安装Python3.5及以上版本)

地址:python官网https://www.python.org/

或者命令行安装python:brew install python3

1.2.2 pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。

目前如果你在 python.org 下载最新版本的安装包,则是已经自带了该工具。

Python 2.7.9 + 或 Python 3.4+ 以上版本都自带 pip 工具。

pip 官网:https://pypi.org/project/pip/

你可以通过以下命令来判断是否已安装:

pip --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

因为我安装的是python2所以:pip install selenium
python3是:pip3 install selenium

安装webdriver
Chrome:我的版本是版本 80.0.3987.149(正式版本) (64 位)
http://chromedriver.storage.googleapis.com/index.html?path=80.0.3987.106/
所以选择版本 80.0.3987.106对应的mac版本下载
webdriver安装路径
Mac:复制webdriver到/usr/local/bin目录下
 
1.3 运用selenium启动浏览器

启动Chrome浏览器:

from selenium import webdriver

browser = webdriver.Chrome() browser.get('http://www.baidu.com/')
具体执行过程如下

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$ 

 
 


 

猜你喜欢

转载自www.cnblogs.com/T-CYP/p/12670682.html