Trying to click a website button with AppleScript - applescript

I wonder if anyone can please help me.
I am trying to use AppleScript to click the buy now button on
https://www.amazon.co.uk/Guinness-World-Records-2022-1/dp/1913484114/ref=zg-bs_books_1/261-5455843-9162427?pd_rd_w=vedUN&pf_rd_p=c3077bff-a471-42bf-a406-b93ec8e1a044&pf_rd_r=G24WF6JCCS60QFBJ54J3&pd_rd_r=b3faca64-d7e3-44a1-9bf8-0c454278bdff&pd_rd_wg=KvFlN&pd_rd_i=1913484114&psc=1
with Safari.
I am very new to all of this but I have spent hours pouring over guides and help with no success.
I have used 'inspect' to find the ID of the button which it says is 'Buy-Now-Button'
So that it is easy for me to test I already have the product page of what I want to buy open in Safari in the background.
Then I run the following script:
tell application "Safari"
activate
delay 2
do JavaScript "document.getElementById'buy-now-button').click();" in document 1
end tell
When I run this script it switches to Safari then nothing happens. No error messages or anything just nothing. I have tried it by the name rather than the ID but I just don't know what is going wrong.
Could anyone help please?

You are missing the opening ( in, document.getElementById'buy-now-button'):
do JavaScript "document.getElementById'buy-now-button').click();" in document 1
So, the following two examples work for me:
tell application "Safari" to do JavaScript "document.getElementById('buy-now-button').click();" in document 1
And:
tell application "Safari" to do JavaScript "document.getElementsByClassName('a-button-input')[1].click();" in document 1

Related

Clicking buttons with AppleScript

I'm trying to learn how to make a script with AppleScript to click on a button on a web page with this script but before the end tell and everything obviously It first needs to open a web page that's the script but I want it to click something on it
here is the script
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"nonya"})
end tell
end tell
I want the clicking button script in this script but before the end tell's
You are right that you need to have the website ready. Once you do, if you know have an ID of the button, you can make AppleScript run a JavaScript in Safari or other browsers:
tell application "Safari"
do JavaScript document.getElementById('buttonID').click();" in document 1
end tell

`AXFocusedUIElement` <- does not get focused element (folder, file), it points to `Finder` menu

trying to reproduce right-click context menu on my Mac.
I found such an article:
https://beebom.com/how-right-click-using-keyboard-mac/
I did accordingly but when I click my keyboard shortcut I get Finder menu not a currently selected file/folder menu.
This is an apple script used,
on run {input, parameters}
tell application "System Events" to set frontApp to name of first process whose frontmost is true
tell application "System Events"
tell application process frontApp
set _selection to value of attribute "AXFocusedUIElement"
tell _selection to perform action "AXShowMenu"
end tell
end tell
return input
end run
Spent hours trying to get this basic and obvious to every Windows user functionality to work, lost of time and very frustrating!
I think code is correct, maybe there is something specific on my computer that stops it from working as expected?
Please help :-)
Not sure i understood what is actually not working for you, but i had problem to reproduce the same script, once i wrote it in Automator.app i tried to press "play" button to see if the script was working and it was telling me something like "syntax error, can't get attribute AXFocusedUIElement of application process Automator" (from memory, not sure it's exactly what was written)
and struggled for a while as well untill i realised there was a pop window that i didn't see, saying me that "automator wants permission to control this computer using accessibility features (this thing in the system preference, security and privacy, privacy, accessibility), so i opened, there was a list of apps allowed to control my computer, i ticked Automator.app and after that it worked
Then every app that i was trying to do a right click was showing me the same pop up and i had to do the same for each one (safari, finder etc...)
And then it worked
Hope might help you!
Encountered same issue.
Allow 'Finder' to control computer at System Preferences>Security & Privacy>Privacy>Accessibility.
Worked for me

Apple Script application control

Thanks for reading my question. I have an issue I was hoping you could help me with. the following apple script will run but ends up timing out because the application is not selected; the icon just bounces on the mac server dock (OSX 10.85). What am I doing wrong?
tell application "(Application Name)"
activate
getURL "(Server URL)"
delay 30
tell database "(database name)"
do script "(Script Name)"
delay 60
close
end tell
end tell
do shell script "(Shell script path)"
Also, I'd like to tell the application to quit prior to running the shell script.
Any and all advice would be appreciated.
THANK YOU!
To quit an app, all you do is put a “quit” command inside its tell block, typically as the last command in the tell block:
tell application "Safari"
quit
end tell
Verify that “getURL” is the right command for the app you are targeting. The modern version is “open location.”
When you run your script from AppleScript Editor, you can tap on “Event Log” at the bottom of the window and see a log of events that occurred during your script’s execution. You can often see what went wrong there.
If your AppleScript is what is timing out, you can use with timeout:
with timeout of 3600 seconds
-- do something within an hour
end timeout
As jweaks said, you need to provide more information to get real help. Every application extends AppleScript in its own way. Your script may be perfect or it may not. There is no way for someone to know without knowing what application your script runs on. It’s like asking for help with Photoshop work but not telling anyone it is Photoshop you are working in.

Get Source of Current Tab in Google Chrome via Applescript

It's child's play to do this in Safari, which has good Applescript support. Google Chrome's AS support has just arrived so I'm giving them the benefit of the doubt. I am basically trying to grab the current HTML via the clipboard so I can get information out. We have some nifty commands like this:
tell application "Google Chrome"
view source of active tab of window 1
save active tab of window 1
print active tab of window 1
reload active tab of window 1
go back active tab of window 1
go forward active tab of window 1
copy selection of active tab of window 1
paste selection active tab of window 1
end tell
but alas you can't say "set X to source of active tab of window 1". Anyone have any suggestions for me? My current ideas are to load the code I need in the background in Safari (pretty ugly) or try to display source and grab it with UI script, but that's also ugly. Also I keep encountering scripting bugs that keep it from working.
Any help would be appreciated.
Since google chrome supports Javascript
--Applescript code
tell active tab of window 1
set sourcehtml to execute javascript
document.getElementsByTagName('html')[0].innerHTML
end tell
Chrome's AppleScript library can execute JavaScript.
This will assign the full source of the page to source and return it
tell application "Google Chrome"
set source to execute front window's active tab javascript "document.documentElement.outerHTML"
end tell
I'm very excited to learn that Chrome has AppleScript support now. It's unfortunate that it's minimal as yet, but I'm sure (I hope!) it'll get better. Since there's no way to get the source directly, I'd choose the following hackish route:
tell application "Google Chrome"
view source of active tab of window 1 -- Or whichever tab you want
delay 3
repeat while loading of active tab of window 1
delay 3
end repeat
select all of active tab of window 1 -- Must *always* be the active tab
copy selection of active tab of window 1
delete tab (active tab index of window 1) of window 1
end tell
delay 1
return the clipboard
Yes, it's hackish, but that's unavoidable, given the current state of the scripting dictionary. The script should be straightforward: open a source tab, wait for it to load, select the contents, copy it, and close the tab. You can play with the delay 3s to see what works best. Note that the first active tab of window 1 is arbitrary, the rest explicitly refer to the source tab. Also, apparently there's no way to close a tab from within Chrome's scripting dictionary (oy vey), so I had to use JavaScript instead. Also, the last delay 1 shouldn't be necessary, but if it wasn't there, my tests would sometimes return the wrong thing, even though the clipboard contents were correct when I pasted them in. I think it's because there was enough text that it took a noticeable amount of time to update the clipboard.
Edit 1: I replaced execute the active tab of window 1 javascript "window.close()" with the delete tab line, as was suggested to me. Unfortunately, delete tab active tab of window 1 doesn't work, so you need this slightly more convoluted construction.
-- This script copies the HTML of a tab to a TextEdit document.
tell application "Chromium"
tell tab 1 of window 1 to view source
repeat while (loading of tab 2 of window 1)
end repeat
tell tab 2 of window 1 to select all
tell tab 2 of window 1 to copy selection
end tell
tell application "TextEdit"
set text of document 1 to the clipboard
end tell
Explanation: The script is put in a tight loop waiting for the tab to load, Then just copies the HTML to clipboard.
tell application "Google Chrome"
set t to active tab index of window 1
tell active tab of window 1 to view source
set t to t + 1
repeat while (loading of tab t of window 1)
end repeat
tell tab t of window 1 to select all
tell tab t of window 1 to copy selection
delete tab t of window 1
end tell
EDIT1: the above script should do exaclt what you want
The simple answer is:
set sourcehtml to execute javascript "document.getElementsByTagName('html')[0].innerHTML"
Seems, other posts are really close but still no clear/working solution. The code working to me:
tell application "Google Chrome"
activate
tell active tab of window 1
set sourcehtml to execute javascript "document.getElementsByTagName('html')[0].innerHTML"
return sourcehtml
end tell
end tell
Seeing as how Chrome's AS support has "just arrived" it is bound to be "exciting" to use. In trying some of the commands they have available in their dictionary, it looks as though they still have some kinks to work out. Until Google exposes a way in the API to get the source code more easily (and/or works out the related kinks), you'll have to use one of the alternatives you mention in your post.

Using AppleScript to copy a webpage TITLE to clipboard

I am looking to copy the webpage title (of an open webpage in safari) to the clipboard using apple script (or a javascript snippet will do).
I will then use that Variable in Automator.
I have looked all over the web, but cannot seem to find the answer.
I just tried this with Safari and it seems to work as expected:
tell application "Safari"
get name of document 1
end tell

Resources