UI自动化常用API:
一、浏览器self.driver = webdriver.Ie()self.driver = webdriver.Firefoxself.driver = webdriver.Chrome二、webdriver 浏览器行为API1. 访问网站self.driver.get(target_url)2. 后退前进self.driver.get(url_1)self.driver...
·
一、浏览器
self.driver = webdriver.Ie()
self.driver = webdriver.Firefox
self.driver = webdriver.Chrome
二、webdriver 浏览器行为API
1. 访问网站
self.driver.get(target_url)
2. 后退前进
self.driver.get(url_1)
self.driver.get(url_2)
self.driver.back()
self.driver.forward()
3.刷新页面
self.driver.refresh()
4. 截屏
self.driver.get_screenshot_as_file(r"c:\file_save_path.png")
4.浏览器窗口相关
#窗口最大化
self.driver.maximize_window()
#获取窗口位置
postion = self.driver.get_window_position()
print postion[x], postion[y]
#设置窗口位置
self.driver.set_window_position(x=400, y=200)
#获取窗口大小
size = self.driver.get_window_size()
print size['width'], size['height']
#设置窗口大小
self.set_window_size(width=400, height=300, windowHandler='current')
三、webdriver 被访问页面相关API
5. 获取页面title
self.driver.title
6. 获取页面HTML源码
self.driver.page_source
7. 获取当前页面url
self.driver.current_url
8. 获取页面指定元素信息、属性值
ele = self.driver.find_element_by_id('username')
#元素tagName
ele.tag_name
#元素大小
ele.size
#元素文字
ele.text
#获取元素属性值
ele.get_attribute("name")
9. 判断页面元素是是否可见、是否可操作
ele = self.driver.find_element_by_id('username')
#判断是否可见
ele.is_displayed()
#判断是否可操作
ele.is_enabled()
10. 输入框操作
ele = self.driver.find_element_by_id('username')
#清除输入框内容
ele.clear()
#输入内容
ele.send_keys(u"你好,我是俊毅")
四、webdriver 被访问页面交互操作 API
#11. 单双击
bt = self.driver.find_element_by_id('button')
#单机页面元素
bt.click()
#双击操作
from selenium.webdriver import ActionChains
ac = ActionChains(self.driver)
ac.double_click(bt).perform()
#拖拽,从一个页面元素拖拽到另一个页面元素的位置上
from selenium.webdriver import ActionChains
start_ele = self.driver.find_element_by_id("1st_ele")
end_ele = self.driver.find_element_by_id("2nd_ele")
ac = ActionChians(self.driver)
ac.drag_and_drop(start_ele, end_ele).perform
#拖拽,按像素拖拽
from selenium.webdriver import ActionChains
ele = self.driver.find_element_by_id("1st_ele")
ac = ActionChians(self.driver)
ac.drag_and_drop_by_offset(ele, 100, 100) #向右,向下拖拽100像素
# 鼠标右键
from selenium.webdriver import ActionChains
ele = self.driver.find_element_by_id("1st_ele")
ac = ActionChians(self.driver)
ac.context_click(ele).perform()
# 鼠标左键长按
from selenium.webdriver import ActionChains
ele = self.driver.find_element_by_id("1st_ele")
ac = ActionChians(self.driver)
ac.click_and_hold(ele).perform()
sleep(3)
ac.release().perform()
# 鼠标悬停
from selenium.webdriver import ActionChains
ele = self.driver.find_element_by_id("1st_ele")
ac = ActionChians(self.driver)
ac.move_to_element(ele).perform()
sleep(2)
四、webdriver 被访问页面特殊交互控件操作 API
#Alert 弹窗
alert = self.driver.switch_to.alert
alert.accept()
#Confirm 弹窗
confirm = self.drier.switch_to.alert
confirm.accept()
confirm.dismiss()
五、webdriver frame操作
#切换至第0个frame
self.driver.switch_to.frame(0)
#切换回默认的frame
self.driver.switch_to.default_content()
#切换至指定id为target的frame
self.driver.switch_to.frame(self.driver.find_element_by_id("target"))
六、新窗口件切换
#获取所有窗口的句柄
all_windows_handlers = self.driver.window_handles
#当前窗口的句柄
print self.driver.current_window_handle
#切换窗口
self.driver.switch_to.window(target_window_handle)
#切换至指定标题的装口
for window_tag in all_windows_handlers:
self.driver.switch_to.window(window_tag)
if self.driver.title == "就是你了":
self.driver.switch_to.window(window_tag)
break;
#切换回默认窗口
self.driver.switch_to.window(all_windows_handlers[0])
七、页面常用控件
#下拉列表1
select_ele = self.driver.find_element_by_id("xxxx")
select_options = select_ele.find_elements_by_tag_name("option")
for option in select_options:
print option.text
print option.get_attribute("value")
option.click()
#下拉列表2
from selenium.webdriver.support.ui import Select
select_ele = Select(self.driver.find_element_by_id("xxx"))
#打印默认选项
print select_ele.first_selected_option.text
#获取所有选项
all_options = select_ele.options
#获取选项是否可用
print all_options[1].enabled()
#获取选项是否被选中
print all_options[1].is_selected()
#获取选项的文本内容
print all_options[1].text
#通过索引选择选项
select_ele.select_by_index(1)
#通过文本内容选择选项
select_ele.select_by_visible_text("哈哈")
#通过value的值选择选项
select_ele.select_by_value("haha")
#单选/复选按钮
bt = self.driver.find_element_by_id("xxx")
bt.click()
print bt.is_selected()
更多推荐
已为社区贡献3条内容
所有评论(0)