问题
I am using Watir with Chromedriver to automate form submission on some website. I have to login and submit multiple forms. The problem is, when I click the submit button the page the page automatically closes, so when I goto('next_url') I get this error:
/Users/jackz/.rvm/gems/ruby-1.9.3-p327/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok': 'auto_id' does not refer to an open tab (Selenium::WebDriver::Error::UnknownError)
The Watir instance is still there, but the window is closed. I could create a new instance every time, but then I would have to login again every time and this would take longer.
So how can I either:
Open a new window in the same Watir instance
or
Suppress the window from closing after I submit
require 'watir-webdriver'
@b = Watir::Browser.new :chrome
@b.goto(URL)
@b.buttons.first.click
#this is when the window closes
@b.goto(NEW_URL)
#then I get an error
Thanks
回答1:
I figured out an answer to my own question. I can open a new window in Watir using javascript:
b = Watir::Browser.new
b.execute_script("window.open()")
b.windows.last.use
This opens a window where I can fill out the form, then when the window automatically closes I still have the original window to work with. Probably not the best solution, but it works for now.
回答2:
What Mrityunjeyan suggested above was in a right direction, but you need to change a few things to make it work.
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {'detach' => true })
b = Watir::Browser.new('chrome', desired_capabilities: caps)
Check the documentation here. https://sites.google.com/a/chromium.org/chromedriver/capabilities
回答3:
Add this to your existing code if your using chrome
. else, modify accordingly as per your browser.
This will keep the window open, and the current watir
session active.
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {'detach' => false})
browser = Watir::Browser.new :chrome
来源:https://stackoverflow.com/questions/14146916/suppress-auto-closing-window-in-watir