2010-02-12 34 views
9

似乎沒有我嘗試過的代碼有任何影響。我的意圖是通過點擊「確定」按鈕來關閉可能出現的任何和所有JavaScript提示。問題是,我的腳本對提示的提示沒有影響。換句話說,它什麼都不做。Ruby Watir:單擊JavaScript提醒上的確定?

這是我有:

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

的HTML:

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

提示中可以更改的文字,我的本意是打OK不論什麼警示/提示窗口內。

PS:我運行Ubuntu。

回答

6

最好的方法是阻止彈出窗口觸發。

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 

參見:http://watirmelon.com/2010/10/31/dismissing-pesky-javascript-dialogs-with-watir/瞭解更多詳情。

0

我覺得你fx.button(:身份證, 「OK」)。點擊等待狀態改變。
但javascript對話框不會改變狀態。
所以你的watir將永遠等待。
如果不是那樣,我不知道。

該動作不會改變狀態,不會返回它。
所以它需要點擊不等待。
當我使用watir(不是firewatir)時,@ ie.button(:id,'OK')。click_no_wait。
然後等待1〜3秒後再彈出。
然後,只要你喜歡。
此外,如果你想控制味精盒(彈出),需要AutoIT。 - 這是等待味精盒樣品,然後點擊確定爲IE popup--

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

這可能是我完全不明白你的意思。 如果這樣忽略這一點。

0

查看/var/lib/gems/1.8/gems/firewatir-1.6.5/unittests/html/JavascriptClick.html(假設這是您的firewatir gem的安裝位置)。我跑了測試,它爲我工作。也許閱讀測試會給你一些關於startClicker應該如何工作的信息。

0

這是問一個永恆前,所以我只是添加的東西多一點更新的爲我做

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

第一個將返回一個布爾 第二個則確定無論您被要求做什麼操作 和第三個將關閉警報沒有