Python Quick Start

1、安装Python

官网下载python: https://www.python.org/

有2.x 3.x版本, 注意,python3.0不向下兼容2.x版本,有很多包3.0不提供

下载完后直接点击安装即可。比如我的安装目录为:C:\Python27

然后配置系统环境:我的电脑 —>属性—>高级—>环境变量—>系统变量

设置Path,将你的python的安装路径 “C:\Python27” 写入Path变量中即可

在控制台打python命令, 出现如下提示表示配置成功:
Python Quick Start

 

2、安装python IDE 

Default: IDLE

Eclipse plugin,(http://pydev.org/updates

1)直接在Eclipse中选择菜单:Help—Install New Updates—And,输入http://pydev.org/updates,下载并安装。

完成后再启动Eclipse,可以在Eclipse菜单Help->About Eclipse SDK->Installation Detail看到PyDev组件的安装

2)在Eclipse菜单Windows->Preferences->PyDev->Interpreter python配置你要只用的python解析器。点击New按钮,从Python的安装路径下选择Python.exe。

 

3、python安装模块(方法一)

Python很多第三方模块可以直接拿来使用

下面以Python language bindings for Selenium WebDriver.为例,说明开发python时,怎么使用第三方提供的对象等

1)https://pypi.python.org/pypi/selenium 下载selenium-2.41.0.tar.gz

2)解压该文件, 再到包含setup.py的目录下执行: (Python第三方模块中一般会自带setup.py文件)

python setup.py install  

 Python Quick Start

表示已经成功install

安装的过程中可能会出现“ImportError: No module named setuptools”的错误提示,这是因为Python默认没有安装setuptools这个模块的,这也是一个第三方模块。下载地址为http://pypi.python.org/pypi/setuptools

先安装setuptools,再重新install selenium即可

注: install完成后, 在C:\Python27\Lib\site-packages 目录下, 多出了selenium-2.41.0-py2.7.egg 目录。

 

4、python安装模块(方法二)

pip 是一个安装和管理 Python 包的工具, 所以python安装模块也可以借助pip工具来直接install, 这种方式可以不用先去下载包, 直接在线完成安装。

借助pip之前, 先安装pip, 地址https://pypi.python.org/pypi/pip#download , 安装pip可采用上面方法一,安装完成后,配置pip环境变量“C:\Python27\Scripts”

安装完pip后, 就可以借助pip 安装、升级、卸载 python第三方模块,如:

pip install -U selenium
pip install goslate
pip install simplejson
pip uninstall simplejson
pip install --upgrade simplejson...... 

5、python开发selenium脚本简单示例

# coding=gb2312

'''

Created on 2014-05-14

@author: jennifer.huang

'''

from selenium import webdriver

from selenium.webdriver.common.by import By



keyword="selenium python"

try:

    browser = webdriver.Chrome("chromedriver.exe")

    browser.get("http://www.baidu.com")

    browser.find_element(By.ID, "kw1").send_keys(keyword)

    browser.find_element(By.ID, "su").submit()

    print browser.find_element(By.ID, "kw").get_attribute("value")

    assert keyword in browser.find_element(By.ID, "kw").get_attribute("value")

except KeyboardInterrupt:

        print "ended by user"

finally:    

    browser.quit()

 

对比下java语言的写法:

 

package com.jennifer.tests;

import junit.framework.Assert;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;



public class SeleniumJava {

    WebDriver driver;

    @Before

    public void init(){

        driver = new FirefoxDriver();

    }

    String keyword="selenium python";

    @Test

    public void test(){

        driver.get("http://www.baidu.com");

        driver.findElement(By.id("kw1")).sendKeys(keyword);

        driver.findElement(By.id("su")).submit();

        System.out.println(driver.findElement(By.id("kw")).getAttribute("value"));

        Assert.assertEquals(driver.findElement(By.id("kw")).getAttribute("value"), keyword);

    }

    @After

    public void tearDown(){

        driver.quit();

    }



}

 

 

 

 

你可能感兴趣的:(python)