1 2 3

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

但是如果想用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

安装webdriver
Chrome:我的版本是版本 80.0.3987.149(正式版本) (64 位)
http://chromedriver.storage.googleapis.com/index.html?path=80.0.3987.106/
所以选择版本 80.0.3987.106对应的mac版本下载
chrome客户端历史版本
webdriver安装路径
Mac:复制webdriver到/usr/local/bin目录下
点击finder,然后右键选择前往文件夹:输入/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   #打开环境变量

 

 

 

 

1.3 运用selenium启动浏览器
首先先用IDEL编辑器试试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$ 

 
 


 

 

posted @ 2020-04-10 00:06  teacher-程  阅读(1695)  评论(0编辑  收藏  举报
levels of contents