简体   繁体   中英

Javascript, how to select option when .selectedIndex is unknown

I am trying to use a combination of applescript and javascript to select and option from a dropdown select menu when the .selectedIndex is not known.

tell application "Safari"
activate
tell document 1
    do JavaScript "document.getElementsByName('dropdown')[0].selectedIndex=2"
end tell
end tell

This would select the 3rd option from a dropbdown menu (jelly) like this:

<select name="dropdown">
<option value="shoe">shoe</option>
<option value="man">man</option>
<option value="jelly">jelly</option>
</select>

However my issue is that the dropdowns are always changing for example, I could go one day and it could look like this:

<select name="dropdown">
<option value="billy">billy</option>
<option value="shoe">shoe</option>
<option value="man">man</option>
<option value="jelly">jelly</option>
</select>

This time the 'man' option would be selected.

Solution:

set valuex to "jelly"
tell application "Safari"
activate
tell document 1
    do JavaScript "document.getElementsByName('dropdown')[0].value='" & valuex & "';"
end tell
end tell

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