问题
I have found several other similar questions, but none of them solve the precise problem I need to solve. I am attempting to simulate a mouse click, to get the entire results of a scroll (e.g., from this site: http://www.deadseascrolls.org.il/explore-the-archive/manuscript/4Q266-1) visible. I specifically want to "click" on the "Load More Results" item at the bottom centre of displayed images (but I think this made more difficult because of the source encoding at the site). I would like to click it until all results are displayed. I have written the following Applescript:
on iaaScroll(myScroll)
tell application "Safari"
activate
open location "http://www.deadseascrolls.org.il/explore-the-archive/manuscript/" & myScroll & "-1" as string
delay 2.0
do JavaScript "var myDIV = document.getElementById('loadmore').getElementsByTagName('span')[0]; myDIV.dispatchEvent(new MouseEvent('mousedown')); myDIV.dispatchEvent(new MouseEvent('mouseup'));" in document 1
end tell
end iaaScroll
I cannot, however, successfully get the JavaScript to load all the results on the page. How would I successfully do this? I do not know JavaScript, so your help is much appreciated.
回答1:
This script will launch that website and click "load more results" one time..
tell application "Safari"
activate
set URL of document 1 to "http://www.deadseascrolls.org.il/explore-the-archive/manuscript/4Q266-1"
delay 3
do JavaScript "document.getElementById('loadmore').click();" in document 1
end tell
Once that webpage is open..This script by itself will click "load more results" once.. Each time you run the script
tell application "Safari"
activate
do JavaScript "document.getElementById('loadmore').click();" in document 1
end tell
Or.. Since that specific page you are talking about ultimately loads 416 images.. I added a repeat statement in the script which will ultimately load that website with all of the images displayed.. You may need to tweak the delay values but as they are set now, the script runs perfectly on my machine
tell application "Safari"
activate
set URL of document 1 to "http://www.deadseascrolls.org.il/explore-the-archive/manuscript/4Q266-1"
delay 4 -- you may need to adjust your delay time to account for your browser speed
repeat 34 times -- adjust this number if there are more than 416 results
do JavaScript "document.getElementById('loadmore').click();" in document 1
delay 0.55 -- you may need to adjust your delay time to account for your browser speed
end repeat
end tell
来源:https://stackoverflow.com/questions/40293474/click-button-on-webpage-with-applescript-javascript