AI教程网 - 未来以来,拥抱AI;新手入门,从AI教程网开始......

9、Selenium+python+API分类总结

Selenium教程 AI君 73℃

Selenium+python+API分类总结

http://selenium-python.readthedocs.org/index.html

分类

方法

方法描述

客户端操作

__init__(self, host, port, browserStartCommand, browserURL)

构造函数。hostselenium
server
ipportelenium serverport,默认为4444browserStartCommand浏览器类型,
iexplorefirefoxbrowserURL:打开的url

start(self)

启动客户端

stop(self)

关闭客户端

鼠标点击

click(self, locator)

点击链接、按钮、单选或者复选框

double_click(self, locator)

双击链接、按钮、单选或者复选框

click_at(self, locator, coordString)

点击链接、按钮、单选或者复选框后调用waitForPageToLoadcoordString是鼠标事件相对于locator返回元素的位置

double_click_at(self, locator, coordString)

双击链接、按钮、单选或者复选框后调用waitForPageToLoadcoordString是鼠标事件相对于locator返回元素的位置

鼠标操作

mouse_over(self, locator)

模拟用户将鼠标悬停在指定元素上

mouse_out(self, locator)

模拟用户将鼠标从指定元素移走

mouse_down(self, locator)

模拟用户按下鼠标键,未释放

mouse_down_at(self, locator, coordString)

模拟用户按下鼠标键,未释放,coordString是鼠标事件相对于locator返回元素的位置

mouse_up(self, locator)

模拟用户释放鼠标键

mouse_up_at(self, locator, coordString)

模拟用户释放鼠标键,coordString是鼠标事件相对于locator返回元素的位置

mouse_move(self, locator)

鼠标拖动指定元素

mouse_move_at(self, locator, coordString)

鼠标拖动指定元素,coordString是鼠标事件相对于locator返回元素的位置

按键操作

key_press(self, locator, keySequence)

模拟用户按下和释放键,keySequence可以是一个字符串(被按下键的ASCII码值);也可以是单独的字符,例如”e”

shift_key_down(self)

按下shift键直到调用doShiftUp()或者载入新页面

shift_key_up(self)

释放shift

alt_key_down(self)

按下alt键直到调用doAltUp()或者载入新页面

alt _key_up(self)

释放alt

control_key_down(self)

按下control键直到调用doControlUp()或者载入新页面

control _key_up(self)

释放control

key_down(self, locator, keySequence)

模拟用户按键不释放,keySequence可以是一个字符串(被按下键的ASCII码值);也可以是单独的字符,例如”e”

key_up(self, locator, keySequence)

模拟用户释放键,keySequence可以是一个字符串(被按下键的ASCII码值);也可以是单独的字符,例如”e”

type(self, locator, value)

设置输入框的值;也可用于设置组合框和复选框的值,所设置的值是勾选的选项,是不可见的文本。

check(self, locator)

选中单选或者复选框

设置输入值

uncheck(self, locator)

取消选中单选或者复选框

单选和复选

is_checked(self, locator)

检查单选或者复选按钮是否选中,如果指定元素不存在或者不是切换按钮则失败

select(self, selectLocator, optionLocator)

使用option locator选择下拉列表的选项;其中option locator有以下几种形式:

1  label=labelPattern:基于label匹配选项,例如可视化文本,*
label=regexp:^[Oo]ther
。(注意label前面的星号)

2  value=valuePattern:基于值匹配选项,* value=other

3  id=id:基于id匹配选项,* id=option1

4  index=index:基于索引(从零开始)匹配选项,* index=2

如果未指定任何前缀,默认情况下是label

selectLocator是下拉列表定位。

add_selection(self, locator, optionLocator)

对于多选元素,在已选选项集合中增加一个选项,locator是多选框定位。

下拉框

remove_selection(self, locator, optionLocator)

对于多选元素,从已选选项集合中删除一个选项,locator是多选框定位。

get_selected_labels(self, selectLocator)

获取下拉列表或者多级下拉列表的多个已选选项的所有label(可视化文本)

get_selected_label(self, selectLocator)

获取下拉列表或者多级下拉列表的某个选项的label(可视化文本)

get_selected_values(self, selectLocator)

获取下拉列表或者多级下拉列表的多个已选选项的所有valuevalue属性)

get_selected_value(self, selectLocator)

获取下拉列表或者多级下拉列表的某个选项的valuevalue属性)

get_selected_indexes(self, selectLocator)

获取下拉列表或者多级下拉列表的多个已选选项的所有indexindex从零开始)

get_selected_index(self, selectLocator)

获取下拉列表或者多级下拉列表的某个选项的indexindex从零开始)

get_selected_ids(self, selectLocator)

获取下拉列表或者多级下拉列表的多个已选选项的所有element ID

get_selected_id(self, selectLocator)

获取下拉列表或者多级下拉列表的某个选项的element ID

is_something_selected(self, selectLocator)

判断下拉列表的某个选项是否选中

get_select_options(self, selectLocator)

获取下拉列表的所有label

set_speed(self, value)

设置操作后的等待时间,以毫秒为单位,设置后作用于每一个selenium操作,默认情况下为0毫秒。

get_speed(self)

获取操作后的等待时间

设置超时或者等待事件

set_timeout(self, timeout)

action设置超时,默认为30秒。一般用于open以及waitFor*。运行错误返回error

wait_for_page_to_load(self, timeout)

等待装载新页面。可以使用该命令代替”AndWait”命令,例如“clickAndWait”, “selectAndWait”,
“typeAndWait”
等(这些命令只在js
API中提供)。Selenium装载新页面时通常会将”newPageLoaded” flag置为true,直到flag恢复为false才继续执行命令。超时后返回error

submit(self, formLocator)

表单提交,在没有提交按钮是非常有用的表单提交接口函数

open(self, url)

打开urlurl可以是相对或者绝对urlopen按钮一直等待页面载入后再处理,其隐式调用“AndWait”。注意:由于安全限制,url的域需要与runner HTMLSelenium
Server
)的域保持一致,如果需要使用不同域的url,需要启动新的浏览器会话。

提交

open_window(self, url, windowID)

如果标识为ID的窗口还未打开,则打开弹出窗口。打开窗口后,需要使用selectWindow命令选择。注意:当window.open调用发生在“onLoad”事件之前或者事件过程时,Selenium无法成功调用window.open,在这种情况下,可以使用SeleniumopenWindow命令强制打开窗口,注意使用空白页面,例如openWindow””,”muFunnyWindow”)。

注意:url可以为空,’windowID’js window ID

窗口操作

select_window(self, windowID)

选择弹出的窗口,选中后所有的命令都在选中窗口中执行。如果想再次选择主窗口,windowID使用nullSelenium根据windowID查找窗口对象的几种策略:1、如果windowIDnull,选中浏览器初始化时打开的最原始的窗口。2、如果windowID是当前应用窗口的js变量名,则该变量包含js window.open()方法的返回值。3、最后,Selenium查找其哈希表,在该表中维护着字符串与窗口对象的映射表,这里的字符串与js方法window.open(url, windowName,
windowFeatures, replaceFlag)
中的第二个参数匹配。如果不知道窗口的名字,可以通过selenium日志查看window.open创建窗口时的名字标识。同open_window,无法成功调用window.open时,可强制性打开空白页面。

select_frame(self, locator)

在当前窗口选择frame或者iframe,可以多次调用该命令选择嵌套的frame。如果想选择parent frame,定位符使用’relative=parent’;如果想选择top frame,使用’relative=top’。也可以使用DOM表达式直接查找frame,例如dom=frames[“main”].frames[“subframe”]

go_back(self)

模拟用户点击浏览器的后退按钮

refresh(self)

模拟用户点击浏览器的刷新按钮

close(self)

模拟用户点击浏览器的关闭按钮

window_focus(self, windowName)

将焦点设置到窗口上,windowName为窗口名

window_maximize(self, windowName)

最大化窗口,windowName为窗口名

get_all_window_ids(self)

返回浏览器打开的所有窗口ID

get_all_window_names(self)

返回浏览器打开的所有窗口名

转载请注明:www.ainoob.cn » 9、Selenium+python+API分类总结

喜欢 (0)or分享 (0)