首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使用selenium+Python配置搭建环境

环境部署

python-2.7.2.msi,python安装程序

setuptools-7.0.zip

pip-1.0.2.tar.gz

selenium-2.18.1.tar.gz(pip命令下载安装),selenium安装程序

selenium-ide-1.6.0.xpi,firefoxWebDriver

selenium-server-standalone-2.18.0.jar,ieWebDriver

chromedriver.exe(chromedriver_win_18.0.1022.0.zip ),chromeWebDriver

安装python

双击安装python-2.7.2.msi即可。建议安装在默认路径:C:Python27

添加环境变量Path:C:Python27

安装验证:cmd命令中,输入python,进入python command line模式

安装setuptools

方法一:

双击安装setuptools-0.6c11.win32-py2.7.exe下一步即可。

安装路径C:Python27Libsite-packages

必须安装setuptools,是因为pip和selenium的安装文件setup.py中使用。

方法二:

进入Python官网https://pypi.python.org/pypi/setuptools/。看到下面的界面

点击ez_setup.py,在浏览器中弹出新的标签页,里面是代码部分

直接打开地址为:https://bootstrap.pypa.io/ez_setup.py

全选并复制到一个新文件命名为ez_setup.py(方便起见保存到桌面)

双击运行,脚本会自动识别Python版本,并下载相应的文件给你安装好,其实挺傻瓜式的。

运行过程

检验

打开Python目录的script文件夹查看

安装 pip

1.在以下地址下载最新的PIP安装文件:http://pypi.python.org/pypi/pip#downloads

2.下载pip-7.1.2.tar.gz (md5, pgp)完成之后,解压pip-7.1.2.tar.gz,将解压文件放到C:Python27下,在DOS环境进入C:Python27pip-1.0.2,执行命令:python setup.py install。请注意安装路径。

3.DOS命令:C:Python27pip-1.0.2> python setup.py install

安装 selenium

进入c:python27scripts

输入 pip install -u selenium (这里u为大写,之前安装的时候 使用小写 导致无法安装)【这步是安装selenium 必须是在联网的情况下才可进行的】

验证

在验证之前需要先 from selenium import webdriver

验证是否已经装好,试着启动firefox浏览器

打开python的idle,运行如下脚本,看运行是否成功。

from selenium import webdriver

browser = webdriver.firefox() # 打开火狐浏览器

配置webdriver

安装firefox webdirver

将selenium-ide-1.6.0.xpi拖进firefox浏览器,即开始安装

Firefox浏览器选装firebug、FirePath插件

配置chrome webdriver

1.解压chromedriver.exe到Python的安装目录下,如C:Python27。

2.添加C:UsersAdministratorAppDataLocalGoogleChromeApplication(chrome安装路径,这里是win7下的安装路径)到环境变量path

配置 ie webdriver

复制IEDriverServer.exe到C:Python27

设置IE浏览器,Internet选线安全,把各模式的“启动保护模式”设置成一样(或者全部启动,或者全部不启动)。

自定义模块

添加环境变量PYTHONPATH,定义为工程所在(自定义功能模块)的路径。

D:workspaceautoprojectsrc

Demo

#!/usr/bin/python

from selenium import webdriver

import time

# Create a new instance of the Chrome driver, Chrome(), Firefox() or Ie() is useable

driver = webdriver.Chrome()

# go to the baidu home page

# find the element that's name attribute is q (the google search box)

inputElement = driver.find_element_by_xpath ("//input[@name='wd']")

submitElement = driver.find_element_by_xpath ("id('su')")

# type in the search

inputElement.send_keys("Cheese!")

# submit the form

submitElement.submit()

# the page is ajaxy so the title is originally this:

print driver.title

driver.quit()

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180206A0LMUJ00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券