Selenium----switch API

Switch

我们在UI自动化测试时,总会出现新建一个tab页面、弹出一个浏览器级别的弹框或者是出现一个iframe标签,这时我们用WebDriver提供的Api接口就无法处理这些情况了。需要用到Selenium单独提供的模块switch_to模块

SwitchToWindows

handles = driver.window_handles
 
# SwitchToWindows接受浏览器TAB的句柄
driver.switch_to.window(handles[1])

SwitchToFrame

# SwitchToFrame支持id、name、frame的element
 
# 接受定位到的iframe的Element,这样就可以通过任意一种定位方式进行定位了
frameElement = driver.find_element_by_name('top-frame')
driver.switch_to.frame(frameElement)
 
# 通过fame的name、id属性定位
driver.switch_to.frame('top-frame')
 
# 当存在多层iframe嵌套时,需要一层一层的切换查找,否则将无法找到
driver.switch_to.frame('top-frame')
driver.switch_to.frame('baidu-frame')
 
# 跳转到最外层的页面
driver.switch_to.default_content()
 
# 多层Iframe时,跳转到上一层的iframe中
driver.switch_to.parent_frame()

SwitchToAlert

# alert 实际上也是Selenium的一个模块
from selenium.webdriver.common.alert import Alert
 
# 也可以通过Webdriver的switch_to来调用
 
# 点击确认按钮
driver.switch_to.alert.accept()
 
# 点击取消按钮
driver.switch_to.alert.dismiss()

# 返回Alert上面的文本内容
text = driver.switch_to.alert.text

猜你喜欢

转载自www.cnblogs.com/HathawayLee/p/10138641.html