简体   繁体   中英

populate input field using AppleScript and JavaScript

I've searched stack overflow but the provided solutions did not work, so I am posting this question:

I've got the following HTML Element:

<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"> 

This is an existing HTML element from a website that I want to fill in using JavaScript.

I am using AppleScript to script this. The script just opens Chrome but does nothing: It should click on a button and populate an input field.

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

I tried this on www.google.de to see if the problem was site-specific, but it does not work either. I want to fetch the country data on that site. It has the following class:

<span class="_Vbu">​Deutschland​</span>​

I tried to call it by classname:

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

I always get the following display dialog content: msng

As noted on the comments, [0] did the trick.

The classname was right as you can see in the following screenshot: www.google.de

This is how it works using 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

This is how it works using 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

If you get the error "myvar is not declared" make sure you address the right document in AppleScript.

It got me the right text in the display dialog ("Deutschland") both times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM