如何在 Ruby 中使用 Selenium Webdriver 关闭弹出窗口 [英] How to dismiss popups with Selenium Webdriver in Ruby
问题描述
我收到这样的弹出窗口...
I am getting popups like this...
...或者像这样...
...or like this...
...通过我的 Selenium 自动化测试,我在 SauceLabs 上远程运行.我尝试了多种禁用或关闭它们的方法,但似乎没有任何效果.我尝试为我的功能添加设置:
...with my Selenium Automation tests I am running remotely on SauceLabs. I tried many ways of disabling them or dismissing them, but nothing seems to work. I tried adding a setting to my capabilities:
autoAcceptAlerts: true
我还尝试切换到驱动程序本身的警报:
I also tried switching to alert with the driver itself:
def click css_selector
begin
browser.find_element(:css, css_selector).click if rendered? css_selector
rescue Selenium::WebDriver::Error::UnknownError
browser.switch_to.alert.accept
click css_selector
end
end
但似乎没有任何效果.我敢肯定之前有人必须处理过这样的事情,有没有运气解决这个问题,最好是跨浏览器"?
But nothing seems to work. I am sure someone had to deal with something like this before, any luck solving this issue, preferably "cross-browser"?
谢谢...
推荐答案
我通过使用 send_key 方法模拟按转义键解决了这个问题:
I fixed this problem by using send_key method to simulate pressing of escape key:
def click css_selector
begin
browser.find_element(:css, css_selector).click if rendered? css_selector
rescue Selenium::WebDriver::Error::UnknownError
dismiss css_selector
click css_selector
end
end
def dismiss selector
browser.find_element(:tag_name, "body").send_keys :escape
end
它在任何方面都不完美,但现在它是一个解决方案,如果您有更好的方法摆脱这些,请告诉我.
Its not perfect in any way, but for now its a solution, if you have better way of getting rid of these, please let me know.
谢谢.
这篇关于如何在 Ruby 中使用 Selenium Webdriver 关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!