Install selenium in python and python selenium

Source: Internet
Author: User
Tags windows x86

Install selenium in python and python selenium
Install python

Go to the Python official website, find "Download", and select your own platform (Windows/Mac) from the drop-down menu. Generally, you do not need to install Python on the Linux platform, open the terminal and enter the python command for verification.

If you are familiar with Python for the first time, you will surely wonder why Python provides two versions: Python2.x and Python3.x? You can use the latest version of Python3.x directly. Because Python2.x is not expected to be maintained by 2020.

If you are a Windows user, why does one version provide 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 networks must be connected during installation; executable file (.exe); embeddable zip file embedded version, which can be integrated into other applications.

Note: During installation, select "Add Python 3.x to PATH". If this option is not selected, set the Python installation directory (for example, C: \ Python36) 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, at the Windows Command Prompt (cmd)/Linux terminal, enter:

C:\Users\name>pipUsage:  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 the prompt "pip is not an internal or external command" is displayed, add the pip installation directory (for example, C: \ Python36 \ Scripts) to the environment variable PATH.

Run the pip command to install Selenium:

C:\Users\name>pip install seleniumCollecting 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……

 

Download the browser driver

After selenium is upgraded to 3.0, different browser drivers are standardized. If you want to use selenium to drive different browsers, you must separately download and set different browser drivers. Of course, the corresponding browser must be available. The browser drivers of different versions correspond to different browser versions. You must select a match when downloading. Personal experience: Chrome and Firefox are both good. Firefox is good for beginners. There are related driver updates on github, which will save some trouble.

Browsers:

Firefox DRIVER: geckodriver

Chrome DRIVER: chromedriver

Internet Explorer DRIVER: IEDriverServer

Edge browser DRIVER: MicrosoftWebDriver

Operabrowser DRIVER: operadriver

PhantomJS browser DRIVER: phantomjs

Note: Some browser driver addresses require scientific Internet access.

 

Set browser driver

Setting the browser address is very simple. You can manually create a directory to store the browser driver, such as C: \ driver, and drop the downloaded browser driver files (such as chromedriver and geckodriver) to this directory.

My computer-> properties-> system settings-> advanced-> environment variables-> system variables-> Path, add the "C: \ driver" directory to the Path value.

  • Path
  • ; C: \ driver

 

Set browser driver

Verify that different browser drivers are working properly.

From selenium import webdriverdriver = webdriver. firefox () # Firefox driver = webdriver. chrome () # Chrome browser driver = webdriver. ie () # Internet Explorer driver = webdriver. edge () # Edge browser driver = webdriver. opera () # operabrowser driver = webdriver. phantomJS () # PhantomJS ......
Test

Open a Python editor and use the built-in Python IDLE by default. Create the baidu. py file and enter the following content:

# Coding = UTF-8
Import time
From selenium import webdriver

Driver = webdriver. Firefox () # Open Firefox

Driver. get ('HTTP: // www.baidu.com ') # Open the Baidu Interface

Driver. find_element_by_id ('kw '). send_keys ('selenium') # enter the content you want to search in the search box

Time. sleep (2) # time required for loading the browser

Driver. find_element_by_id ('su '). click () # search complete


When your browser opens automatically and the following screen appears, congratulations on setting up the python and selenium environments.


:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.