2014-01-30 59 views
0

我意識到有幾個關於此的線程,他們都是非常具體的某些網站。我的背景是Python,而不是Javascript或Applescript,我對如何實現這個動作感到困惑。使用Javascript | Applescript單擊Safari中的按鈕

我見過幾個腳本做這個動作,即:

tell application "Safari" 

activate 

do JavaScript "document.forms['search']['order_list_search_batch'].click()" 

end tell 

什麼是擔當了這一點,最好的方法是什麼?

我是在"document.forms[WHATGOESHERE?].click()"之間

我試圖點擊http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi繼續按鈕發生的事情感到困惑。

我又繼續按鈕「檢查元素」,並得到這個代碼:

<input style="cursor: pointer;" value="" name="proceed" class="proceed" onmouseover="this.style.cursor=&quot;pointer&quot;" type="submit"> 

我怎麼知道要放什麼東西在腳本中單擊基於檢查元素結果掉這個按鈕?我想明白,所以我可以在多個案例中使用這種方法。這裏沒有href鏈接。

enter image description here

當前的代碼不工作

tell application "Safari" to activate 
open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi" 
delay 3 
tell application "Safari" to do JavaScript "document.forms[0].elements[document.forms[0].elements.length-1]" 

回答

3
如果檢查PR

document.forms['search']['order_list_search_batch'].click() 

改變你的JavaScript查詢

document.forms['form']['proceed'].click() 

點擊按鈕,您會發現它位於具有「form」屬性的「name」屬性的表單元素中。該按鈕本身具有「名稱」屬性,其值爲「繼續」。因此,在JavaScript中,括號之間的第一個術語查找名爲「form」的表單,第二個術語在「form」-form內引用名爲「proceed」的元素。如果這是有道理的;-)

此外,你將不得不告訴AppleScript在哪個文件執行JavaScript。如果沒有打開其他選項卡,您剛剛打開的文檔可能是文檔1。

希望這會有所幫助!

整個代碼:

tell application "Safari" 
activate 
open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi" 
delay 3 
do JavaScript "document.forms['form']['proceed'].click()" in document 1 
end tell 
1

你可以使用JavaScript DOM功能getElementsByName(),它會返回與該名稱的所有元素的數組,你可以(在這種情況下)目標中的第一項。

此外,您需要專門定位您要執行javascript的選項卡以使其在Safari中正常工作。

見下

tell application "Safari" 
    activate 
    open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi" 
    delay 3 
    do JavaScript "document.getElementsByName('proceed')[0].click();" in current tab of first window 
end tell 
相關問題