selenium 使用笔记
2015-10-10 11:55
113 查看
下面一段代码是使用selenium访问网页一个小实例
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Dec 6, 2013
@author: Jay <smile665@gmail.com>
@description: use PhantomJS to parse a web page to get the geo info of an IP
'''
# from selenium import webdriver
# import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')
# driver = webdriver.PhantomJS(executable_path='./phantomjs') #这要可能需要制定phatomjs可执行文件的位置
# driver.get("http://www.ip.cn/125.95.26.81")
# #print driver.current_url
# #print driver.page_source
# print driver.find_element_by_id('result').text.split('\n')[0].split('来自:')[1]
# driver.quit
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since
from selenium.webdriver.common.by import By
# browser = webdriver.Firefox() # Get local session of firefox # 使用火狐浏览器,调用的时候会打开游览器执行完代码后会自动关闭
browser = webdriver.PhantomJS(executable_path='phantomjs') # 这要可能需要制定phatomjs可执行文件的位置
browser.get("http://192.168.0.125:9600/web/test/") # Load page
try:
WebDriverWait(browser, 10).until(EC.title_contains("complete")) # 查找数据,500毫秒查找一次,找到则继续执行下面代码,超过10秒抛出异常
except Exception, e:
print e
browser.quit()
ele = browser.find_element_by_id('math_box')
print browser.find_element_by_id('math_box').get_attribute('innerHTML')
# browser.close() # 关闭当前焦点所在的窗口
browser.quit() # 调用dispose()方法,关闭所有窗口,并且安全关闭session
在windows上面使用webdriver.Firefox() 需要先将firefox安装路径添加到环境变量中。
windows使用webdriver.Ie() 驱动,需要先下载IE驱动https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver,将解压后的IEDriverServer.exe复制到python安装路径。
selenuim介绍以及相关应用"使用python/casperjs编写终极爬虫"
官网教程:http://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-webdriver
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Dec 6, 2013
@author: Jay <smile665@gmail.com>
@description: use PhantomJS to parse a web page to get the geo info of an IP
'''
# from selenium import webdriver
# import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')
# driver = webdriver.PhantomJS(executable_path='./phantomjs') #这要可能需要制定phatomjs可执行文件的位置
# driver.get("http://www.ip.cn/125.95.26.81")
# #print driver.current_url
# #print driver.page_source
# print driver.find_element_by_id('result').text.split('\n')[0].split('来自:')[1]
# driver.quit
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since
from selenium.webdriver.common.by import By
# browser = webdriver.Firefox() # Get local session of firefox # 使用火狐浏览器,调用的时候会打开游览器执行完代码后会自动关闭
browser = webdriver.PhantomJS(executable_path='phantomjs') # 这要可能需要制定phatomjs可执行文件的位置
browser.get("http://192.168.0.125:9600/web/test/") # Load page
try:
WebDriverWait(browser, 10).until(EC.title_contains("complete")) # 查找数据,500毫秒查找一次,找到则继续执行下面代码,超过10秒抛出异常
except Exception, e:
print e
browser.quit()
ele = browser.find_element_by_id('math_box')
print browser.find_element_by_id('math_box').get_attribute('innerHTML')
# browser.close() # 关闭当前焦点所在的窗口
browser.quit() # 调用dispose()方法,关闭所有窗口,并且安全关闭session
在windows上面使用webdriver.Firefox() 需要先将firefox安装路径添加到环境变量中。
windows使用webdriver.Ie() 驱动,需要先下载IE驱动https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver,将解压后的IEDriverServer.exe复制到python安装路径。
selenuim介绍以及相关应用"使用python/casperjs编写终极爬虫"
官网教程:http://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-webdriver
相关文章推荐
- UVA-1152 4 Values whose Sum is 0 (二分)
- m
- 解析时已到达文件结尾
- HTML的文本输入框、密码输入框
- Part 8 Coalesce function in sql server
- Part 9 Union and union all in sql server
- mysql免安装方法
- Go之简单并发
- TPP是也只是一种挑战
- PHP中JSON的应用技巧
- Do you agree with me?
- yii 操作mysql数据库实操
- Intellij IDEA配置
- 什么时候使用接口?什么时候使用抽象?选择Java接口还是抽象类
- android ndk 提高编译数度(cpu 多核编译)
- HanziToPinyin类的使用及碰到的问题
- 浅谈Oracle 11g 发行版2 新安装后关于登录的一些基本操作
- MVC 自定义 错误页面
- 黑马程序员—Foundation框架之NSDictionary与NSMutableDictionary
- axis和xfire以及CXF三种方式实现WebServices分别有什么优缺