[英]populate input field using AppleScript and JavaScript
我搜索了堆棧溢出,但是提供的解決方案不起作用,所以我發布了這個問題:
我有以下HTML元素:
<input xmlns="http://www.w3.org/1999/xhtml" id="loginform:fPers" type="text" name="loginform:fPers" value class="inputFieldApp" maxlength="20" tabindex="4" title="Some title">
這是我想使用JavaScript填寫的網站中現有的HTML元素。
我正在使用AppleScript編寫腳本。 該腳本僅打開Chrome,但不執行任何操作:應單擊一個按鈕並填充輸入字段。
tell application "Google Chrome"
activate
set thescript to "document.getElementById(\"loginform:kommenbtn\").click();"
set thescript2 to "document.getElementById(\"loginform:fPers\").value = \"112233\";"
execute javascript thescript2
end tell
我在www.google.de上嘗試過此操作,以查看問題是否特定於站點,但也無法解決。 我想在該網站上獲取國家/地區數據。 它具有以下類別:
<span class="_Vbu">Deutschland</span>
我試圖用類名來稱呼它:
tell application "Google Chrome"
activate
set thescript to "document.getElementByClassName('_Vbu').innerHTML"
set myvar to execute active tab of front window javascript thescript
display dialog myvar
end tell
我總是得到以下顯示對話框內容:msng
正如評論中指出的, [0]
。
使用Google Chrome瀏覽器的工作方式如下:
tell application "Google Chrome"
activate
set thescript to "document.getElementsByClassName('_Vbu')[0].innerHTML;"
set myvar to execute active tab of front window javascript thescript
display dialog myvar
end tell
這是使用Safari的工作方式:
tell application "Safari"
set thescript to "document.getElementsByClassName('_Vbu')[0].innerHTML;"
tell document 1
set myvar to do JavaScript thescript
end tell
display dialog myvar
end tell
如果出現錯誤“未聲明myvar”,請確保您在AppleScript中找到了正確的文檔。
兩次都在顯示對話框(“德國”)中為我提供了正確的文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.