How to handle new browser pop up after clicking a js button in Watir?

狂风中的少年 提交于 2019-12-08 12:20:32

问题


I am trying to using watir in ruby, I am able open a browser, enter some values to a username/password form, but then I press enter, then I click the submit button (actually it is pressing an enter, it is easiler to code), it would pop up a new browser windows for our application, then I found I have no control to the new browser. What can I do?

In addition to my problem, the new browser window got no menubar, no toolbar, no navigation bar, thus I cannot open the IE developer toolbar to find the name of the element in the webpage in the new browser.

By the way, my app can only support IE.


I tried the attach method, but it does work:

C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.8.1/lib/watir/ie-class.rb:302:

in `attach_browser_window': Unable to locate a window with title of (?-mix:New browser title) (Watir::Exception::NoMatchingWindowFoundException)

    from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.8.1/lib/watir/ie-class.rb

:150:in `_attach_init'

    from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.8.1/lib/watir/ie-class.rb

:144:in `attach' from test1.rb:36


回答1:


Attach method is able to handle new window. http://rdoc.info/gems/watir/1.8.1/Watir/IE.attach

for example

browser = Watir::Browser.new
browser.text_field(:id,'username').set 'username')
browser.button(:index,1).click
# popped up the new window
popup = Watir::Browser.attach(:title,'Foobar')

If you will use only the popped up window, you can overwrite browser variable instead.




回答2:


You could try to authenticate like so:

examplehost.com/authentication?user=foo&password=bar

You could also try to remove "target" attribute (which is probably equal to "_blank") of that authentication form ( i'm pretty much sure you should be able to achive this with watir )

EDIT:

Also, there's this option:

ie2 = Watir::IE.attach(:title, ‘New Ie window, opened upon form submit’)

it can also attach by :url instead of :title




回答3:


ok, there's possibly this solution that might work:

b = Watir::Browser.start "http://yourpage"

b.execute_script("document.getElementById('your-image-id').onClick = function() {}")

so, the idea is to get that image and overwrite it's onclick event



来源:https://stackoverflow.com/questions/5630084/how-to-handle-new-browser-pop-up-after-clicking-a-js-button-in-watir

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