0

I'm filling a webpage using Applescript / Javascript,

one of them is a dropdown menu, which I fill with this code :

document.querySelector('select#reasonPopUpButton').value = '18';

the issue is the index change time to time.

Can I select the good menu using the title instead of the index?

Here what I tried so far :

tell application "Safari"
    do JavaScript "document.querySelector('select#reasonPopUpButton').value = 'Unauthorized purchase';" in current tab of window 1

end tell
  • I'd try using the .innerText or .innerHTML property, e.g. document.querySelector('select#reasonPopUpButton').innerText = "Unauthorized purchase"; ... – user3439894 Jan 27 at 17:59
  • Thanks for your reply, I tried document.querySelector('select#reasonPopUpButton').innerText = 'Unauthorized purchase'; but then the dropdown menu become small with no value at all inside – Kevin Jan 27 at 18:40
  • How about with .innerHTML instead, did you get the same results? Can you provide the actual URL of the page the dropdown menu is on? – user3439894 Jan 27 at 19:19
  • same, that remove all the option, only the index (.value = '18';) seems to work. worst case I'll leave it like this, thank you anyway – Kevin Jan 27 at 19:25
  • Can you provide the actual URL of the page the dropdown menu is on? – user3439894 Jan 27 at 19:48
1

You can filter the list of options for one with the text you're looking for, then select that one. This can be achieved with the following JavaScript:

Array.from(document.querySelector('select#reasonPopUpButton').children)
  .find(el => el.textContent == "Unauthorized purchase")
  .selected = true;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .