个人笔记,摘抄自虫师python selenum,仅供个人参考
1、安装:
pip install selenium
下载webdriver
# webdriver 下载并放置在python主目录 1.chromedriver 下载地址:https://code.google.com/p/chromedriver/downloads/list 2.Firefox的驱动geckodriver 下载地址:https://github.com/mozilla/geckodriver/releases/ 3.IE的驱动IEdriver 下载地址:http://www.nuget.org/packages/Selenium.WebDriver.IEDriver/
2、基本使用
from selenium import webdriver browser = webdriver.Firefox() browser = webdriver.Chrome() browser.set_window_size(480, 800) #参数数字为像素点 browser.maximize_window() #将浏览器最大化显示 browser.get(url) browser.back() browser.forward() browser.close() # browser.quit()
3、元素定位
# 单个元素 browser.find_element_by_id() browser.find_element_by_name() browser.find_element_by_tag_name() browser.find_element_by_class_name() browser.find_element_by_css_selector() browser.find_element_by_xpath() # 多个元素 browser.find_elements_by_[...]() #层级定位 #点击Link1链接(弹出下拉列表) dr.find_element_by_link_text(‘Link1‘).click() #找到id 为dropdown1的父元素 WebDriverWait(dr, 10).until(lambda the_driver: the_driver.find_element_by_id(‘dropdown1‘).is_displayed()) #在父亲元件下找到link为Action的子元素 menu = dr.find_element_by_id(‘dropdown1‘).find_element_by_link_text(‘Action‘) #鼠标定位到子元素上 webdriver.ActionChains(dr).move_to_element(menu).perform() # 鼠标移到悬停元素上 ActionChains(browser).move_to_element(ele).perform()
操作对象:
- · click 点击对象
- · send_keys 在对象上模拟按键输入
- · clear 清除对象的内容,如果可以的话
WebElement 另一些常用方法:
- · text 获取该元素的文本
- · submit 提交表单
- · get_attribute 获得属性值
5、定位frame
多层框架或窗口的定位:
- switch_to_frame()
- switch_to_window()
智能等待:
- implicitly_wait()
调用js方法
- execute_script(script, *args)
在当前窗口/框架 同步执行javaScript
脚本:JavaScript的执行。
*参数:适用任何JavaScript脚本。
使用:
driver.execute_script(‘document.title’)
7、selenium 对下拉框信息的定位,以及对警告提示框的处理
本节重点
- 处理下拉框
- switch_to_alert()
- accept()
js="var q=document.getElementById(‘id‘).scrollTop=10000" driver.execute_script(js) js="var q=document.documentElement.scrollTop=10000" driver.execute_script(js)
9、模拟键盘按键操作
- l 键盘按键用法
- l 键盘组合键用法
- l send_keys() 输入中文运行报错问题
from selenium.webdriver.common.keys import Keys #需要引入keys包 driver.find_element_by_id("user_name").send_keys(Keys.TAB) driver.find_element_by_id("user_pwd").send_keys(Keys.ENTER) driver.find_element_by_id("kw").send_keys(Keys.CONTROL,‘a‘) driver.find_element_by_id("kw").send_keys(Keys.CONTROL,‘c‘) driver.find_element_by_id("kw").send_keys(Keys.CONTROL,‘v‘) send_keys(u"输入中文")
module:selenium.webdriver.common.keys ? class Keys() – NULL = u’ue000’ – CANCEL = u’ue001’ # ^break – HELP = u’ue002’ – BACK_SPACE = u’ue003’ – TAB = u’ue004’ – CLEAR = u’ue005’ – RETURN = u’ue006’ – ENTER = u’ue007’ – SHIFT = u’ue008’ – LEFT_SHIFT = u’ue008’ # alias – CONTROL = u’ue009’ – LEFT_CONTROL = u’ue009’ # alias – ALT = u’ue00a’ – LEFT_ALT = u’ue00a’ # alias – PAUSE = u’ue00b’ – ESCAPE = u’ue00c’ – SPACE = u’ue00d’ – PAGE_UP = u’ue00e’ – PAGE_DOWN = u’ue00f’ – END = u’ue010’ – HOME = u’ue011’ – LEFT = u’ue012’ – ARROW_LEFT = u’ue012’ # alias – UP = u’ue013’ – ARROW_UP = u’ue013’ # alias – RIGHT = u’ue014’ – ARROW_RIGHT = u’ue014’ # alias – DOWN = u’ue015’ – ARROW_DOWN = u’ue015’ # alias – INSERT = u’ue016’ – DELETE = u’ue017’ – SEMICOLON = u’ue018’ – EQUALS = u’ue019’ – NUMPAD0 = u’ue01a’ # numbe pad keys – NUMPAD1 = u’ue01b’ – NUMPAD2 = u’ue01c’ – NUMPAD3 = u’ue01d’ – NUMPAD4 = u’ue01e’ – NUMPAD5 = u’ue01f’ – NUMPAD6 = u’ue020’ – NUMPAD7 = u’ue021’ – NUMPAD8 = u’ue022’ – NUMPAD9 = u’ue023’ – MULTIPLY = u’ue024’ – ADD = u’ue025’ – SEPARATOR = u’ue026’ – SUBTRACT = u’ue027’ – DECIMAL = u’ue028’ – DIVIDE = u’ue029’ – F1 = u’ue031’ # function keys – F2 = u’ue032’ – F3 = u’ue033’ – F4 = u’ue034’ – F5 = u’ue035’ – F6 = u’ue036’ – F7 = u’ue037’ – F8 = u’ue038’ – F9 = u’ue039’ – F10 = u’ue03a’ – F11 = u’ue03b’ – F12 = u’ue03c’ – META = u’ue03d’ – COMMAND = u’ue03d’
- driver.get_cookies() 获得cookie信息
- add_cookie(cookie_dict) 向cookie添加会话信息
- delete_cookie(name) 删除特定(部分)的cookie
- delete_all_cookies() 删除所有cookie
ActionChains 类
- context_click() 右击
- double_click() 双击
- drag_and_drop() 拖动
from selenium.webdriver.common.action_chains import ActionChains #定位到要双击的元素 qqq =driver.find_element_by_xpath("xxx") #对定位到的元素执行鼠标双击操作 ActionChains(driver).double_click(qqq).perform() #定位元素的原位置 element = driver.find_element_by_name("source") #定位元素要移动到的目标位置 target = driver.find_element_by_name("target") #执行元素的移动操作 ActionChains(driver).drag_and_drop(element, target).perform()