selenium2.0关于python的常用函数
新建实例driver = webdriver.Chrome()
1.通过标签属性Id查找元素
方法:find_element_by_id(element_id)
实例:driver.find_element_by_id("iptUsername")
2.通过标签属性name查找元素
方法:find_element_by_name(element_name)
实例:driver.find_element_by_id("inputPwname")
3.通过标签Xpath路径查找元素
方法:find_element_by_xpath(xpath)
实例:driver.find_element_by_xpath("//div[@id='menuContainer']/div/div/ul/li[4]/a")
4.通过标签名tagname查找元素
方法:find_element_by_tag_name(tag_name)
实例:driver.find_element_by_tag_name("input")
注意:通过tag_name查找时使用find_element_by_tag_name查找到的是第一个标签的tag_name
5.通过标签中的元素文本链接查找元素
方法:find_element_by_link_text(link_text)
实例:driver.find_element_by_link_text('登 录')
6.通过标签的class属性查找元素
方法:find_elements_by_class_name(class_name)
实例:driver.find_elements_by_class_name("x-panel-body")
7.通过css样式查找元素
方法:find_element_by_css_selector()
实例:driver.find_element_by_css_selector("input.btn") 温馨提示:find_elements_by_id()查询到的是一个集合,如果id名字重复的时候可以采取find_elements_by_id(),其他的查询方式同理;
8.浏览器中加载url
方法:get(url)
实例:driver.get("http//:www.baidu.com")
9.向前
方法:forward()
实例:driver.forward()
10.返回当前会话中的cookies
方法:get_cookies()
实例:driver.get_cookies()
11.根据cookie name 查找
方法:driver.get_cookie(cookie_name)
实例:driver.get_cookie("NET_SessionId")
12.截取当前页面
方法:
get_screenshot_as_file(filename)实例:driver.get_screenshot_as_file("D:\\Program Files\\Python27\\NM.bmp")13.获取当前窗口的坐标方法:get_window_position()实例:driver.get_window_position()14.获取当前窗口的长和宽方法:get_window_size()实例:driver.get_window_size()
新建实例driver = webdriver.Chrome()
1.获取当前页面的Url函数
方法:current_url
实例:
driver.current_url
2.获取元素坐标
方法:location
解释:首先查找到你要获取元素的,然后调用location方法
实例:
driver.find_element_by_xpath("//*[@id='tablechart']/tbody/tr[14]/td[9]").location
3.表单的提交
方法:submit
解释:查找到表单(from)直接调用submit即可
实例:
driver.find_element_by_id("form1").submit()
4.获取CSS的属性值
- 方法:value_of_css_property(css_name)
- 实例:
- driver.find_element_by_css_selector("input.btn").value_of_css_property("input.btn")
- 5.获取元素的属性值
- 方法:get_attribute(element_name)
- 实例:
- driver.find_element_by_id("sellaiyuan").get_attribute("sellaiyuan")
- 6.判断元素是否被选中
- 方法:is_selected()
- 实例:
- driver.find_element_by_id("form1").is_selected()
- 7.返回元素的大小
- 方法:size
- 实例:
- driver.find_element_by_id("iptPassword").size
- 返回值:{'width': 250, 'height': 30}
- 8.判断元素是否显示
- 方法:is_displayed()
- 实例:
- driver.find_element_by_id("iptPassword").is_displayed()
9.判断元素是否被使用 - 方法:is_enabled()
- 实例:
- driver.find_element_by_id("iptPassword").is_enabled()
- 10.获取元素的文本值
- 方法:text
- 实例:driver.find_element_by_id("iptUsername").text
- 11.元素赋值
- 方法:send_keys(*values)
- 实例:
- driver.find_element_by_id("iptUsername").send_keys('admin')
- 注意如果是函数需要增加转义符u,eg.
- driver.find_element_by_id("iptUsername").send_keys(u'青春')
- 12.返回元素的tagName
- 方法:tag_name
- 实例:
- driver.find_element_by_id("iptUsername").tag_name
- 13.删除浏览器所以的cookies
- 方法:delete_all_cookies()
- 实例:
- driver.delete_all_cookies()
- 14.删除指定的cookie
- 方法:delete_cookie(name)
- 实例:deriver.delete_cookie("my_cookie_name")
- 15.关闭浏览器
- 方法:close()
- 实例:driver.close()
- 16.关闭浏览器并且推出驱动程序
- 方法:quit()
- 实例:driver.quit()
- 17.返回上一页
- 方法:back()
- 实例:driver.back()
- 18.设置等待超时
- 方法:implicitly_wait(wait_time)
- 实例:driver.implicitly_wait(30)
- 19.浏览器窗口最大化
- 方法:maximize_window()
- 实例:driver.maximize_window()
- 20.查看浏览器的名字
- 方法:name
- 实例:drvier.name
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’
ActionChains 类不仅仅是只包含了上面的三个方法,下面将方法列出:
class ActionChains(driver)
driver:The WebDriver instance which performs user actions.
Generate user actions. All actions are stored in the ActionChains object. Call perform() to fire stored actions.
– perform()
Performs all stored actions.
– click(on_element=None)
Clicks an element.
on_element:The element to click. If None, clicks on current mouse position.
– click_and_hold(on_element)
Holds down the left mouse button on an element.
on_element:The element to mouse down. If None, clicks on current mouse position.
– context_click(on_element)
Performs a context-click (right click) on an element.
on_element:The element to context-click. If None, clicks on current mouse position.
– double_click(on_element)
Double-clicks an element.
on_element:The element to double-click. If None, clicks on current mouse position.
– drag_and_drop(source, target)
Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
source:The element to mouse down.
target: The element to mouse up.
– key_down(key, element=None)
Sends a key press only, without releasing it. Should only be used with modifier keys (Control, Alt andShift).
key:The modifier key to send. Values are defined in Keys class.
element:The element to send keys. If None, sends a key to current focused element.
– key_up(key, element=None)
Releases a modifier key.
key:The modifier key to send. Values are defined in Keys class.
element:The element to send keys. If None, sends a key to current focused element.
– move_by_offset(xoffset, yoffset)
Moving the mouse to an offset from current mouse position.
xoffset:X offset to move to.yoffset:Y offset to move to.
– move_to_element(to_element)
Moving the mouse to the middle of an element.
to_element: The element to move to.
– move_to_element_with_offset(to_element, xoffset, yoffset)
Move the mouse by an offset of the specificed element. Offsets are relative to the top-left corner of the
element.
to_element: The element to move to.xoffset:X offset to move to.yoffset:Y offset to move to.
– release(on_element)
Releasing a held mouse button.
on_element:The element to mouse up.
– send_keys(*keys_to_send)
Sends keys to current focused element.
keys_to_send:The keys to send.
– send_keys_to_element(self, element,*keys_to_send):
Sends keys to an element.
element:The element to send keys.keys_to_send:The keys to send.