Watir Webdriver 对JS Dialog的处理

Urania ·
更新时间:2024-09-20
· 594 次阅读

Watir webdriver内置了如何处理javascript. dialog的方法,以及从dialog获得所需值的方法。

# 判断alert是否存在 browser.alert.exists?   # 获得alert的值 browser.alert.text   # 关闭alert browser.alert.ok browser.alert.close

# 接受confirm browser.alert.ok   # 取消confirm browser.alert.close

# 输入内容到prompt browser.alert.set "Prompt answer"   # 接受prompt browser.alert.ok   # 取消prompt browser.alert.close

如果上述方法无效,我们还有一些替代的方法:

# 对于alert,overide从而使其不返回任何值 browser.execute_script("window.alert = function() {}")   # 返回用户在prompt输入的值 browser.execute_script("window.prompt = function() {return 'my name'}")   # 返回空值,用于模拟点击prompt的cancel browser.execute_script("window.prompt = function() {return null}")   # 返回true,用于模拟模拟点击confirm的ok browser.execute_script("window.confirm = function() {return true}")   # 返回false,用于模拟点击confirm的cancel browser.execute_script("window.confirm = function() {return false}")   # 对于离开popup不返回任何值 browser.execute_script("window.onbeforeunload = null")



dialog webdriver js

需要 登录 后方可回复, 如果你还没有账号请 注册新账号