Ruby with Watir: Handling javascript popup window

与世无争的帅哥 提交于 2019-12-20 05:58:11

问题


I have a problem with watir and javascript popup window

here's my test script

require 'watir'

browser = Watir::Browser.start "/url/"

    browser.link(:text, /Add New Blog/).wait_until_present
    browser.link(:text, /Add New Blog/).click

    // Here is where the javascript window popup
    window = browser.ie.Document.ParentWindow

    browser.window(:title, /Blog/) do
    browser.text_field(:id, /text title of Blog/).set 'Watir'
    browser.select_list(:id, /dropdownlist type/).select ("News")
    browser.button(:value, /Save/).click
    end

The problem is after window popup shows, it's unable to locate the element of the text_field in window Blog. I also have try this but it says that => in 'window': wrong number of arguments (2 for 1) (ArgumentError)

browser.window(:title => "annoying popup").use do
  browser.button(:id => "close").click
end

Any solution? and btw Im using ruby 1.9.3. Appreciate your help.TQ


回答1:


There are two forms of Watir - watir-classic and watir-webdriver. If possible/convenient you should use watir-webdriver because it is the future of browser automation and where all the development focus will be going forward.

Your code looks fine if you are using watir-webdriver. I have never used watir-classic, but it appears that it only supports initializing a window with a hash instead of multiple arguments. So maybe this will work for you if you need to use watir-classic:

browser.window(title: /Blog/) { #execute block }



来源:https://stackoverflow.com/questions/27419253/ruby-with-watir-handling-javascript-popup-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!