English 中文(简体)
Ruby Watir: Clicking OK on JavaScript Alerts?
原标题:

Seems none of the code I ve tried has any affect. My intention is to close any and all JavaScript prompts that may come up by hitting the "OK" button. Problem is, my script has no affect on the prompts that come up. In other words, it does nothing.

Here s what I have:

fx = FireWatir::Firefox.start(somepage)
fx.startClicker("OK")
fx.button(:id, "OK").click
fx.button(:id, "CONFIRM").click

The HTML:

<script type="text/javascript">
    alert("Alert!");
    window.confirm("Confirm?");
</script>

The text in the prompts can change, my intention is to hit OK regardless of what is inside the alert/confirm prompt.

PS: I m running Ubuntu.

最佳回答

The best way is to stop pop-ups from triggering at all.

require  watir 
b = Watir::Browser.start "http://somepagewithdialogs"
# don t return anything for alert
b.execute_script("window.alert = function() {}")

# return some string for prompt to simulate user entering it
b.execute_script("window.prompt = function() {return  my name }")

# return null for prompt to simulate clicking Cancel
b.execute_script("window.prompt = function() {return null}")

# return true for confirm to simulate clicking OK
b.execute_script("window.confirm = function() {return true}")

# return false for confirm to simulate clicking Cancel
b.execute_script("window.confirm = function() {return false}")

# interact with some element which would trigger the pop up
b.button(:id => "dialogTrigger").click

See: http://watirmelon.com/2010/10/31/dismissing-pesky-javascript-dialogs-with-watir/ for more detail.

问题回答

this was asked an eternity ago so I m just adding something a little more updated that did it for me

@browser.alert.exists? @browser.alert.ok @browser.alert.close

first one will return a boolean second one will ok whatever action you are prompted to do and third one will close the alert with no

Pop ups are black magic to me. Did you try the solutions from here?

I would also suggest posting your question at watir-general.

I think your fx.button(:id, "OK").click was waiting state changed.
But javascript dialog does not change state.
So your watir will be waiting forever.
If not like that,I do not know.

The action will not change state, never return it.
So it needs click no wait.
When I use watir(not firewatir), @ie.button(:id, OK ).click_no_wait.
Then better wait 1~3 second for popup.
Then as you like.
And moreover if you want control msg-box(popup), need to AutoIT. --This is sample for wait msg-box and click ok for IE popup--

autoit=WIN32OLE.new( AutoItX3.Control )
autoit.WinWait( Windows Internet Explorer )
autoit.WinActive( Windows Internet Explorer )
autoit.ControlClick( Windows Internet Explorer ,  , OK )

It is possible that completely I don t understand what you mean. If so ignore this.

Check out /var/lib/gems/1.8/gems/firewatir-1.6.5/unittests/html/JavascriptClick.html (assuming that s where your firewatir gem is installed). I ran the test and it worked for me. Maybe reading the test will give you some insight into how startClicker is supposed to work.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签