selenim
一、安装selenium
- Pip install selenium==2.53.1 (稳定版)
- 下载火狐浏览器35.0.1 http://dl.pconline.com.cn/download/54846-1.html
- 添加组件:firepath(0.9.7.1.1),firebug(2.019)
- Forfox ide https://addons.mozilla.org/zh-CN/firefox/addon/selenium-ide/
- Chorm http://blog.csdn.net/huilan_same/article/details/51896672 mac Chorm . https://www.applex.net/downloads/google-chrome-for-mac.25/history
- Firefox http://ftp.mozilla.org/pub/mozilla.org/firefox/releases/
- 谷歌浏览器驱动:http://npm.taobao.org/mirrors/chromedriver/
二、Selenium的简单操作
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
print(driver.current_url)
print(driver.title)
# 页面最大化
driver.maximize_window()
# 页面大小的获取和设置
print(driver.get_window_size())
driver.set_window_size(400,400)
# 页面位置
print(driver.get_window_position())
driver.set_window_position(100,100)
url_zhihu = 'http://zhuanlan.zhihu.com'
driver.get(url_zhihu)
# 回退
driver.back()
# 前进
driver.forward()
driver.close()
三、Selenium的元素定位
- Id
el_input = driver.find_element_by_id('kw')
el_input.send_keys('selenium')
el_click = driver.find_element_by_id('su')
el_click.click()
- Name
el_input = driver.find_element_by_name('email')
- Class_name
el_input = driver.find_element_by_class_name('shark-pager-next')
- Tag_name(不建议)
el_input = driver.find_element_by_tag_name("input")
- Link text(针对a标签)
el_input = driver.find_element_by_link_text("房屋出租")
el_input.click()
driver.quit()
- Partial link text(针对a标签)
el_input = driver.find_element_by_partial_link_text("hao123")
el_input.click()
driver.quit()
- Xpath(鼠标右键-FireBug)
el_input = driver.find_element_by_xpath(".//*[@id='screening']/div[1]/h2/span[1]/a")
print(driver.get_attribute('title'))
- Css selector(鼠标右键-复制唯一选择器)
el_input = driver.find_element_by_css_selector(".inner-con2 > a:nth-child(2) > img:nth-child(1)")
el_input.click()
driver.quit()
四、selenium方法总结
B=webdriver.Firefox()
B.get(‘http://baidu.com’)
B.title
B.current_url
Clear
Send_keys
Click
Submit
Back
Size
Id
五、selenium鼠标事件
Context_click 右击事件
double_click 双击事件
Drag_and_drop 拖动
Move_to_element() 鼠标停在一个元素上
Click_and_hold 按下鼠标左键在一个元素上
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
b = webdriver.Firefox()
b.get('https://www.jikexueyuan.com/')
position = b.find_element_by_css_selector('.aside-cList > li:nth-child(1)')
ActionChains(b).move_to_element(position).perform()
dian = b.find_element_by_partial_link_text('React Native')
dian.click()
time.sleep(3)
b.quit()
六、Selenium键盘事件
send_keys(Keys.BACKSPACE) 退格键
send_keys(Keys.CONTROL,’a’) 全选
send_keys(Keys.CONTROL,’v’) 粘贴
send_keys(Keys.CONTROL,’c’) 复制
send_keys(Keys.CONTROL,’x’) 剪切
send_keys(Keys.ENTER) 回车
>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> b.get('https://www.jikexueyuan.com/')
>>> data = b.find_element_by_id('searchinput')
>>> data.send_keys('python')
>>> data.send_keys('python1')
>>> data.send_keys(Keys.BACKSPACE)
七、Selenium多窗口切换
b.window_handles 显示所有句柄(列表)
b.current_window.handles显示当前句柄
b.switch_to_window(b.window_handles[0])切换句柄
b.current_url 当前地址
b.close() 关闭当前
b.quit() 关闭所有
八、Selenium alert
switch_to_alert() 返回alert对象
accept() 确认
dismiss() 取消
send_keys() 有输入框才能使用,否则报错
>>> obj=b.switch_to_alert()
>>> obj.accept()
九、Selenium excel
Import xlrd
Xl =xlrd.open_workbook(‘test.xls’) #打开excel
Table = xl.sheets()[0] #通过索引获取工作表
Row = table.row_values(0) #获取第一行内容
Table.nrows #行数
Table.ncols #列数
Table.cell(0,0).value 单元格
Import xlswriter
Xl =xlswriter.workbook(‘test.xls’) #创建excel
Table=xl.add_worksheet(‘sheet1’) #添加sheet
Table.write_string(0,0,’data’) / Table.write_string(‘A1’,’data’) #写
Table.set_column(‘C:E’,15) #设置单元格大小
Xl.close() #关闭