Applescript | Javascript - Click on Link using Jav

2019-05-30 05:54发布

问题:

I am trying to get applescript to click on the (link), I have posted the code below, i have tried many ways but i am unable to get it work. Also the link is different every time so i cant just copy that link into it. Thanks in advanced! Also This is all the code to do with it (I think) i want it to click on the date of last upload: (Link)

<div class="span4 offset1">
                            <ul class="unstyled">
                                <li>Monthly Views: <strong class="pull-right"><a href="#" class="data-na" data-original-title="" title="">NA</a></strong></li>
                                <li>Daily New Views: <strong class="pull-right">18,830</strong></li>
                                <li>Daily New Subscribers: <strong class="pull-right">33</strong></li>
                                <li>Total Views: <strong class="pull-right">298,481</strong></li>
                                <li>Total Channel Views: <strong class="pull-right">3,467</strong></li>
                                <li>Total Subscribers: <strong class="pull-right">649</strong></li>
                                <li id="date-of-last-upload">Date of Last Upload: <a href="https://www.youtube.com/watch?v=YVrV-OmDJcw&amp;feature=youtube_gdata" target="_BLANK">(link)</a><strong class="pull-right">14-01-10 @ 08:18:36 pm</strong></li>
                                <li class="clearfix">Partner:
                                    <strong class="pull-right">
                                                                                                                                                                TheDeeman25+user                                                                                                                    </strong>
                                </li>
                            </ul>
                        </div>

回答1:

I'm not sure if this is the answer you're looking for but you can get the value of the href attribute of the anchor tag by getting childNodes. The value returned by JavaScript is the URL you want to set your document eventually. Instead of using a click I get the desired location and set the URL of document 1 to it; the result/goal will be the same as a click.

tell application "Safari"
    tell document 1
        set URL of it to do JavaScript "document.getElementById('date-of-last-upload').childNodes[1].href"
    end tell
end tell

EDIT: to open the url in a new tab (if opening links in tab is set to automatic in preferences) you can change the set URL of it to into open location

tell application "Safari"
    tell document 1
        open location (do JavaScript "document.getElementById('date-of-last-upload').childNodes[1].href")
    end tell
end tell