python3-selenium-UI自动化入门二

###

接上一篇

###

一、消息窗

#切换到页面的消息窗

a = driver.switch_to_alert

a.text#消息窗文本

a.accept()#相当于点击“确定”

a.dismiss()#相当于点击“取消”

 

二、切换页面框架

例如

baidu = driver.find_element_by_css_selector('iframe#id')#先定位要切换的框架

driver.switch_to.frame(baidu)#切换

 

#切换回上一级框架

driver.switch_to.parent_frame()

 

#切换到最外层框架

driver.switch_to.default_content()

 

三、切换浏览器窗口

#获得打开的所有的窗口句柄

window = driver.window_handles

 

#切换到当前最新打开的窗口

driver.switch_to.window(window[-1])

 

四、鼠标操作

from selenium.webdriver.common.action_chains import ActionChains

1、悬停

#先定义一个实例

action = ActionChains(driver)

#添加一个事件,el为定位好的元素

action.move_to_element(el)

#执行添加好的事件

action,perform()

 

2、左键按住不放

action = ActionChains(driver)

#el元素按住3s,然后释放

action.click_and_hold(el).press(3).release().perform()

 

3、右键点击

action = ActionChains(driver)

#action.context_click(el).perform()

 

4、拖拽

action = ActionChains(driver)

#把元素el拖到那个坐标上

action.drag_and_drop_by_offset(el,500,600).perform()

 

 

四、js点击元素

driver.execute_script("arguments[0].click();",el)

 

posted @ 2019-08-26 15:07  Apple_li  阅读(429)  评论(0编辑  收藏  举报