Search code examples
rubyseleniumselenium-webdriveralertsaucelabs

How to dismiss popups with Selenium Webdriver in Ruby


I am getting popups like this...

enter image description here

...or like this...

enter image description here

...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"?

Thanks...


Solution

  • 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.

    Thanks.