码迷,mamicode.com
首页 > 编程语言 > 详细

Selenium2+Python:Webdriver API速记手册

时间:2017-01-15 19:30:06      阅读:518      评论:0      收藏:0      [点我收藏+]

标签:自动   pos   表单   单选   双击   weakref   键盘输入   property   鼠标事件   

  由于web自动化常常需要控制浏览器行为和操作页面元素,相关函数又比较多,于是再此记下一份Webdriver API查阅文档以备不时之需。

  参考:虫师《Selenium2自动化测试实战》,和http://selenium-python.readthedocs.io/api.html

1 #coding=utf-8
2 from selenium import webdriver
3 driver=webdriver.Firefox()
4 driver.get(http://www.baidu.com/)
5 print driver attributes:
6 print dir(driver)
7 Elem=driver.find_element_by_id(kw)
8 print WebElement attributes:
9 print dir(Elem)

  输出结果:

driver attributes:
[__class__, __delattr__, __dict__, __doc__, __format__, __getattribute__, __hash__, __init__, __module__, __new__,
__reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__, __weakref__, _file_detector
, _is_remote, _mobile, _switch_to, _unwrap_value, _wrap_value, add_cookie, application_cache, back, capabilities,
close, command_executor, create_options, create_web_element, current_url, current_window_handle, delete_all_cookies,
delete_cookie, desired_capabilities, error_handler, execute, execute_async_script, execute_script, file_detector,
file_detector_context, find_element, find_element_by_class_name, find_element_by_css_selector, find_element_by_id,
find_element_by_link_text, find_element_by_name, find_element_by_partial_link_text, find_element_by_tag_name,
find_element_by_xpath, find_elements, find_elements_by_class_name, find_elements_by_css_selector, find_elements_by_id,
find_elements_by_link_text, find_elements_by_name, find_elements_by_partial_link_text, find_elements_by_tag_name,
find_elements_by_xpath, forward, get, get_cookie, get_cookies, get_log, get_screenshot_as_base64,
get_screenshot_as_file, get_screenshot_as_png, get_window_position, get_window_size, implicitly_wait, launch_app,
log_types, maximize_window, mobile, name, orientation, page_source, quit, refresh, save_screenshot, service,
session_id, set_page_load_timeout, set_script_timeout, set_window_position, set_window_size, start_client, start_session,
stop_client, switch_to, switch_to_active_element, switch_to_alert, switch_to_default_content, switch_to_frame,
switch_to_window, title, w3c, window_handles]

  调用语法:driver.属性名(注:Some attributes are callable (or methods) and others are non-callable (properties),以下列出的均为方法属性)

  常用属性:

 1 driver.find_element_by_id(id_)                        #查找元素的id 
 2 driver.find_element_by_name(name)                    #查找元素的name
 3 driver.find_element_by_link_text(link_text)           #查找元素的链接文本
 4 driver.find_element_by_partial_link_text(link_text)   #查找元素的链接的部分文本
 5 driver.find_element_by_tag_name(name)                 #查找元素的标签名
 6 driver.find_element_by_xpath(xpath)                   #查找元素的xpath
 7 driver.find_elements_by_xpath(xpath)                  #查找元素内的子元素的xpath 
 8 driver.find_element_by_class_name(name)               #查找一个元素的类名
 9 driver.find_elements_by_class_name(name)              #查找元素的类名
10 driver.find_element_by_css_selector(css_selector)     #查找并返回一个元素的CSS 选择器
11 driver.find_elements_by_css_selector(css_selector)    #查找并返回多个元素的CSS 选择器列表
 1 driver.get(url)                          #浏览器加载url。
 2 driver.forward()                         #浏览器向前(点击向前按钮)
 3 driver.back()                            #浏览器向后(点击向后按钮)
 4 driver.refresh()                         #浏览器刷新(点击刷新按钮)
 5 driver.close()                           #关闭当前窗口,或最后打开的窗口。
 6 driver.quit()                            #关闭所有关联窗口,并且安全关闭session
 7 driver.maximize_window()                 #最大化浏览器窗口
 8 driver.set_window_size(宽,高)            #设置浏览器窗口大小
 9 driver.get_window_size()                 #获取当前窗口大小
10 driver.switch_to_window(窗口句柄)         #切换到新窗口
11 driver.switch_to_frame(id或name属性值)    #切换到新表单(同一窗口)。若无id或属性值,可先通过xpath定位到iframe,再将值传给switch_to_frame()
1 driver.get_screenshot_as_file(filename)           #窗口截图
2 driver.switch_to_alert()                          #警告框处理。处理JavaScript所生成的alert,confirm,prompt.
3 driver.implicitly_wait(秒)                 #隐式等待,通过一定的时长等待页面上某一元素加载完成。若提前定位到元素,则继续执行。若超过时间未加载出,则抛出NoSuchElementException异常
4 driver.execute_script(js)                         #调用js
5 driver.get_cookies()                              #获取当前会话所有cookie信息。
6 driver.get_cookie(cookie_name)                    #返回字典的key为“cookie_name”的cookie信息。
7 driver.add_cookie(cookie_dict)                    #添加cookie。“cookie_dict”指字典对象,必须有name和value值。
8 driver.delete_cookie(name,optionsString)         #删除cookie信息。
9 driver.delete_all_cookies()                       #删除所有cookie信息

  

输出结果: 

WebElement attributes:
[__class__, __delattr__, __dict__, __doc__, __eq__, __format__, __getattribute__, __hash__, __init__, __module__,
__ne__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__,
__weakref__, _execute, _id, _parent, _upload, _w3c, clear, click, find_element, find_element_by_class_name,
find_element_by_css_selector, find_element_by_id, find_element_by_link_text, find_element_by_name,
find_element_by_partial_link_text, find_element_by_tag_name, find_element_by_xpath, find_elements,
find_elements_by_class_name, find_elements_by_css_selector, find_elements_by_id, find_elements_by_link_text,
find_elements_by_name, find_elements_by_partial_link_text, find_elements_by_tag_name, find_elements_by_xpath,
get_attribute, id, is_displayed, is_enabled, is_selected, location, location_once_scrolled_into_view, parent, rect,
screenshot, screenshot_as_base64, screenshot_as_png, send_keys, size, submit, tag_name, text, value_of_css_property]

  调用语法:driver.find_element*.属性名(注:Some attributes are callable (or methods) and others are non-callable (properties),以下列出的均为方法属性)

  常用属性:

1 driver.find_element_by_*().clear()                   #清除文本
2 driver.find_element_by_*().send_keys(value)          #模拟键盘输入
3 driver.find_element_by_*().click()                   #单击元素,可以作用于按钮、链接、复选框、单选框、下拉框
4 driver.find_element_by_*().submit()                  #用于表单提交,应用范围不及click()广泛
5 driver.find_element_by_*().get_attribute(name)      #获得属性值
6 driver.find_element_by_*().is_displayed()           #返回元素结果是否可见(True 或 False)
7 driver.find_element_by_*().is_selected()           #返回元素结果是否被选中(True 或 False)
8 driver.find_element_by_*().find_element_by_*()       #定位元素,用于二次定位

 

补充:鼠标事件:perform()对所有鼠标事件进行存储,或者说是对所以操作都进行了提交

 
 #定位到要右击的元素
 element =driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
 #对定位到的元素执行鼠标右键操作
 ActionChains(driver).context_click(qqq).perform()
 
 #定位到要双击的元素
 element =driver.find_element_by_id("submit")
 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()
 
  #定位鼠标悬停的元素
  element = self.find_element(*locator)
 ActionChains(self.driver).move_to_element(element).perform()

 

Selenium2+Python:Webdriver API速记手册

标签:自动   pos   表单   单选   双击   weakref   键盘输入   property   鼠标事件   

原文地址:http://www.cnblogs.com/Ryana/p/6204339.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!