Benches macOS

8

Applescript / Javascript Help

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I have a script which, among other things, navigates to the first result in an Amazon search result page, and then extracts some information from the page. Up until today this has worked great, now it appears that Amazon has changed the coding of their page and my script is broken. I have been playing around with it for an hour now and cannot get it to work. Some help would be greatly appreciated!

Here is the element inspection using an example item:

Here is the section of my code as it sits currently:

tell application "Safari"
	activate
	open location "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=" & upc
	delay 2
	do JavaScript "document.getElementById('search_result_0"').getElementsByTagName('a')[0].click()" in document 1

I see where the page references data-cel-widget="search_result_0", but I cannot figure out how to access it to get my script to click that first item. The script does also try the second item in the list if the first one is not the correct item, so I do need to be able to reference each item individually.

Any ideas? Thank you in advance!

8 replies

Sign in or join Beachball Help to reply.

  1. 0

    rccharles

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I found this easier to program:

    helpAvailable = document.querySelector(

    "section.create-post div.toolbar div.row div#rte-help-menu ul.rte-help-menu-list")

    You fill in what you see in the bottom "Path to tag".

    You can test in console mode.

  2. 0

    rccharles

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You could change to something like:

    document.querySelector("img.s-image")

    There are a few errors, but my query is at the bottom.

    document.querySelector("img.s-image")
    

    example of using querySelector:

    // While the path to the help icons are different, the html coding 
        // ends up the same. 
        helpAvailable = document.querySelector(
                "section.create-post div.toolbar div.row div#rte-help-menu ul.rte-help-menu-list")
        if (debug>=2) console.log ("helpAvailable for create discussion is ", helpAvailable)
        // Should be available since input editor is already displayed.
        if (helpAvailable) { expandHelp(helpAvailable) }
    
    

    R

  3. 0

    rccharles

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I found this easier to program:

    helpAvailable = document.querySelector(

    "section.create-post div.toolbar div.row div#rte-help-menu ul.rte-help-menu-list")

    You fill in what you see in the bottom "Path to tag".

    You can test in console mode.

  4. 0

    rccharles

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You could change to something like:

    document.querySelector("img.s-image")

    There are a few errors, but my query is at the bottom.

    document.querySelector("img.s-image")
    

    example of using querySelector:

    // While the path to the help icons are different, the html coding 
        // ends up the same. 
        helpAvailable = document.querySelector(
                "section.create-post div.toolbar div.row div#rte-help-menu ul.rte-help-menu-list")
        if (debug>=2) console.log ("helpAvailable for create discussion is ", helpAvailable)
        // Should be available since input editor is already displayed.
        if (helpAvailable) { expandHelp(helpAvailable) }
    
    

    R

  5. 0

    rccharles

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    "document.getElementById('search_result_0"').getElementsByTagName('a')[0].click()" in document 1
    

    Problems:

    1) search_result_0  // does not exist
    
    2) _0"')  // double quote is out of place get rid of. 
    

    R

  6. 0

    AppleScriptQuestions

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thank you for your help and very detailed responses! This query works to find the image, but I can't seem to get the script to click the link attached to the image...and if I did, how could I script it to pick the second image in the list as opposed to the first if necessary?

  7. 0

    AppleScriptQuestions

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    R -- your suggestions led me down the right path! I was able to use the data attributes with the query selector!

    This is what finally worked:

    do JavaScript "document.body.querySelector('div [data-index="0"]').getElementsByTagName('a')[0].click()" in document 1
    

    I can change the index value to get the second & third results.

    Thank you again for your help!

  8. 0

    rccharles

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Super! I'm glad you found the pattern that worked.

    R