Home  >  Article  >  Backend Development  >  Sharing the installation process of selenium under python

Sharing the installation process of selenium under python

黄舟
黄舟Original
2017-10-08 09:31:542209browse

Install python

Open Python official website, find "Download", select your own platform (Windows/Mac) in the drop-down menu, the general Linux platform already comes with Python, so there is no need to It needs to be installed. Verify it by opening "Terminal" and entering the "python" command.

If you are coming into contact with Python for the first time, you will definitely be confused as to why Python provides two versions: Python2.x and Python3.x? Then, just use the latest version of Python3.x. Because Python2.x is not expected to be maintained until 2020.

If you are a Windows platform user, you will encounter why a version provides multiple download links. For example:

  • Python 3.6.1 - 2017-03-21

    • ##Download Windows x86 web-based installer

    • Download Windows x86 executable installer

    • Download Windows x86 embeddable zip file

    • Download Windows x86-64 web- based installer

    • Download Windows x86-64 executable installer

    • Download Windows x86-64 embeddable zip file

    • Download Windows help file

x86 only supports 32-bit systems; x86-64 supports 64-bit systems. web-based requires an Internet connection during the installation process; executable executable file (.exe) installation method; embeddable zip file embedded version, which can be integrated into other applications.

Note: During the installation process, you need to check: "Add Python 3.x to PATH". If not, you need to add the Python installation directory (such as: C:\Python36 ) added to the environment variable PATH.

Open the Windows command prompt (cmd)/Linux terminal and enter:

C:\Users\name>pythonPython 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.
>>>

Install selenium

First, enter the Windows command prompt (cmd)/Linux terminal:

C:\Users\name>pip

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
……

Make sure that the pip command is available. If it prompts "pip is not an internal or external command", you need to add the pip installation directory (such as: C:\Python36\Scripts) to the environment variable PATH.

接下来通过pip命令安装Selenium:

C:\Users\name>pip install selenium
Collecting selenium
  Downloading selenium-3.4.3-py2.py3-none-any.whl (931kB)    
  26% |████████                       | 245kB 576kB/s eta 0:00:02    
    27% |█████████                      | 256kB 570kB/s eta 0:00:02    
    28% |██████████                     | 266kB 536kB/s eta 0:00:0    
    29% |███████████                    | 276kB 530kB/s eta 0:00:0    
    30% |████████████                   | 286kB 586kB/s eta 0:00:0
……

 

下载浏览器驱动

当selenium升级到3.0之后,对不同的浏览器驱动进行了规范。如果想使用selenium驱动不同的浏览器,必须单独下载并设置不同的浏览器驱动。当然对应的浏览器必须有啦,不同版本的浏览器驱动对应不同的浏览器版本,下载时要选匹配的哦。个人经验:Chrome和Firefox都是不错的,初学就用Firefox好了,github上有相关驱动的更新,会省去一些麻烦。

各浏览器下载地址:

Firefox浏览器驱动:geckodriver

Chrome浏览器驱动:chromedriver

IE浏览器驱动:IEDriverServer

Edge浏览器驱动:MicrosoftWebDriver

Opera浏览器驱动:operadriver

PhantomJS浏览器驱动:phantomjs

注:部分浏览器驱动地址需要科学上网。

 

设置浏览器驱动

设置浏览器的地址非常简单。 我们可以手动创建一个存放浏览器驱动的目录,如: C:\driver , 将下载的浏览器驱动文件(例如:chromedriver、geckodriver)丢到该目录下。

我的电脑–>属性–>系统设置–>高级–>环境变量–>系统变量–>Path,将“C:\driver”目录添加到Path的值中。

  • Path

  • ;C:\driver

 

设置浏览器驱动

验证不同的浏览器驱动是否正常使用。

from selenium import webdriver


driver = webdriver.Firefox()   # Firefox浏览器

driver = webdriver.Chrome()    # Chrome浏览器

driver = webdriver.Ie()        # Internet Explorer浏览器

driver = webdriver.Edge()      # Edge浏览器

driver = webdriver.Opera()     # Opera浏览器

driver = webdriver.PhantomJS()   # PhantomJS

……

测试

打开一款Python编辑器,默认Python自带的IDLE也行。创建 baidu.py文件,输入以下内容:

<code class="language-python hljs"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-string"># coding=utf-8<br/>import time  <br/>from selenium import webdriver  <br/>  <br/>driver = webdriver.Firefox() #打开火狐浏览器  <br/>  <br/>driver.get(&#39;http://www.baidu.com&#39;) #打开百度界面  <br/>  <br/>driver.find_element_by_id(&#39;kw&#39;).send_keys(&#39;selenium&#39;)  #在搜索框内输入想要搜索内容  <br/>  <br/>time.sleep(2)  # 浏览器加载需要时间<br/>  <br/>driver.find_element_by_id(&#39;su&#39;).click()  #搜索完成 <br/><br/><br/>当你的浏览器自动打开,并出现以下画面时,恭喜完成python与selenium的环境搭建<br/><br/><br/>效果图:<img    style="max-width:90%"  style="max-width:90%" alt="" src="https://img.php.cn/upload/article/000/000/194/79d53031447e1e560f831c4705775039-0.png"/><br/><br/></span></span></span></code>

The above is the detailed content of Sharing the installation process of selenium under python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn