Selenium + Python automated test environment setup

Source: Internet
Author: User
Tags seleniumhq

Selenium's build on the Python platform:

Build platform Windows

The preparation tools are as follows:

-------------------------------------------------------------

Download python

http://python.org/getit/

Download Setuptools "Python's Basic Package Tool" (Can download EXE installation package directly from Baidu)

# Http://pypi.python.org/pypi/setuptools

https://pypi.python.org/packages/2.7/s/setuptools/

Download Pip "Python's installation package management tool"

Https://pypi.python.org/pypi/pip

-------------------------------------------------------------

Because the version is updated, Pyhton select 2.7.xx, Setuptools choose the version of your platform, Pip do not worry about tar.gz as available under Windows.

Window Installation steps:

1, Python installation, this does not explain, EXE file run installation can, since you choose Python, I believe you are familiar with Python, I install directory C:\Python27

2, Setuptools installation is also very simple, the same EXE file, the default will find the Python installation path, will be installed in the C:\Python27\Lib\site-packages directory.

Via the Setuptools connection provided above, drag the page to the bottom to find, setuptools-1.3.2.tar.gz file (version will be updated with time version), unzip the file, find Ez_ install.py file, enter the Windows command prompt to execute ez_install.py:

C:\setuptools-1.3>python ez_install.py No error indicates successful installation.

(If you are prompted that Python is not an internal or external command!) To configure the environment variable)

Modify My Computer, properties, advanced-environment variable--the path in the system variable is:

Variable name: PATH

Variable value:; C:\Python27

3, install PIP, I am the default decompression in the C:\pip-1.3.1 directory

4. Open a command prompt (start---cmd return) Enter the C:\pip-1.3.1 directory:

C:\pip-1.3.1 > Python setup.py install

5, then switch to the C:\Python27\Scripts directory input:

C:\Python27\Scripts > Easy_install pip

6, Installation Selenium, (: Https://pypi.python.org/pypi/selenium)

If it is connected, you can enter the command installation directly under C:\Python27\Scripts:

C:\Python27\Scripts > Pip install-u Selenium

If there is no Internet connection (this is generally not possible), download Selenium 2.33.0 (currently the latest version)

and unzip the entire directory into the C:\Python27\Lib\site-packages directory.

======= If you do not analyze the wedriver principle, the following two steps can be omitted =============

7. Download and install (Http://www.java.com/zh_CN/download/chrome.jsp?locale=zh_CN) what? You have not done Java, refer to other documents! It's not hard.

8. Download Selenium server (https://code.google.com/p/selenium/) found in the list on the left side of the page

Selenium-server-standalone-xxx.jar

Right! This is the thing, download it and unzip it;

In the Selenium-server-standalone-xxx.jar directory, use the command Java-jar Selenium-server-standalone-xxx.jar to start (if not open, to see if the port is occupied: netstat -aon|findstr 4444).

===============================================

How to install under Ubuntu:

1. Installation: Setuptools

[Email protected]:~# apt-get Install Python-setuptools

2. Install Pip

[Email protected]:/home/fnngj/python# tar-zxvf pip-1.4.1.tar.gz

[Email protected]:/home/fnngj/python# cd PIP-1.4.1/

[Email protected]:/home/fnngj/python# python setup.py Install

3, installation Selenium

[Email protected]:/home/fnngj/python/pip-1.4.1# pip Install-u Selenium

Congratulations! Your early work has been done, the above steps are really a bit cumbersome, but not difficult, but we have finished, let's experience the results! Take the example on the Python website:

From selenium import Webdriver

From selenium.common.exceptions import nosuchelementexception

From Selenium.webdriver.common.keys import keys

Import time

Browser = Webdriver. Firefox () # Get local session of Firefox

Browser.get ("http://www.yahoo.com") # Load page

Assert "Yahoo!" in Browser.title

Elem = Browser.find_element_by_name ("P") # Find the Query box

Elem.send_keys ("SELENIUMHQ" + Keys.return)

Time.sleep (0.2) # Let the page load, would be is added to the API

Try

Browser.find_element_by_xpath ("//a[contains (@href, ' http://seleniumhq.org ')]")

Except nosuchelementexception:

Assert 0, "can ' t find SELENIUMHQ"

Browser.close ()

(If an error occurs during the run:

Webdriverexception:message:u ' unexpected error launching Internet Explorer.

Protected Mode settings is not the same for all zones. Enable Protected Mo

De must is set to the same value (enabled or disabled) for all zones. '

To change the Internet options for IE, safely, remove the Enable protection mode from the internet/local internet/trusted standing/restricted site, or all the hooks. )

-----------------------------------------

A good document for selenium + python

Http://selenium.googlecode.com/git/docs/api/py/index.html

=========================== If you want to run a script from another browser (IE Chrome) =================================

Install Chrome driver

Chrome driver is here.

   1.download unzip and you'll get aChromedriver.exefile (I click to open, run the hintstarted no prot 9515, what's this for? Port9515is it accounted for? In the middle of the day), later only to know the need to put this guyChromeunder the installation directory... \google\chrome\application\,Then SetPathenvironment variables, putChromethe installation directory (my:C:\Program files\google\chrome\application), and then call run:

# coding = Utf-8

From selenium import Webdriver

Driver = Webdriver. Chrome ()

Driver.get (' http://radar.kuaibo.com ')

Print Driver.title

Driver.quit ()

And made a mistake:

Chrome version must be >= 27.0.1453.0\n (Driver info:chromedriver=2.0,platform=windows NT 5.1 SP3 x86)

said that my Chrome version is not larger than 27.0.1453.0 , this good to do, update to the latest version can be.

Installing IE Driver

In the new version of Webdriver , only IE driver is installed to test the work using ie .

IE driver here, remember to download the corresponding driveraccording to the operating system version of your machine.

For the time being, it should be similar to how Chrome is installed.

Remember to configure the protection mode of IE

If you want to use Webdriver to start IE, then you need to configure the protection mode of IE.

The protection mode in IE is selected or all can be ticked off.

Selenium + Python automated test environment setup

Related Article

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.