[有人可以告诉我如何编写一个Applescript来将鼠标移动或悬停在Safari的按钮上吗?我已经成功制作了一个Applescript,可以单击该按钮,但是我不知道如何使鼠标移到那里而不是单击。因为我需要从网站上下载一些图像,这些图像仅在将鼠标悬停在该按钮上时才会显示(无法单击该按钮)。
执行脚本时我想获取信息的网站(HTML元素:https://shopee.vn/Bút-line-đi-nét-chuyên-nghiệp-SAKURA-PIGMA-MICRON-12-size-(0.03-Brush)-BÁN-LẺ-i.22061868.886461468
我成功单击按钮的Applescript(此操作单击到左侧的第二个图像:]
tell application "Safari"
do JavaScript "document.getElementsByClassName('_3ZDC1p')[2].click();" in document 1
end tell
或:
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
clickClassName("_3ZDC1p", 2)
所以您能告诉我如何以类似于上述脚本的方式将鼠标悬停在按钮上(但不要单击)吗?请注意,上面网站中的“ 1.0图形”按钮已褪色,这意味着无法单击它,但是一旦将鼠标悬停在该按钮上,就会出现左侧图像,我想让该图像变得如此糟糕。我只需要拥有一个可以将鼠标悬停在该按钮上的脚本即可看到图像。谢谢!
我不知道使用基本香草AppleScript本身来move鼠标光标的方法;但是,有第三方实用程序可以做到,一个是cliclick;但是,您还需要能够告诉它将其移至何处,这有其自身的问题。
在不同的轨道上,查看OP中URL的网页,我看到的是对target element的HTML。 "document.getElementsByClassName('_3ZDC1p')[2].innerHTML;"
对于image,有一个URL。
为什么不获取HTML,解析它以获得URL,然后将其直接下载到您的系统中?
以下示例 AppleScript 代码就是这样:
-- # Get the HTML for the target element.
tell application "Safari" to ¬
tell front document to ¬
set innerHTML to do JavaScript ¬
"document.getElementsByClassName('_3ZDC1p')[2].innerHTML;"
-- # Parse 'innerHTML' to get the URL.
set text item delimiters to {"(", ")"}
set theURL to second text item of innerHTML
set text item delimiters to {}
-- # Set the pathfilename for the saved image,
-- # e.g,: /Users/me/Pictures/1591894262.jpeg
-- # Name is based on seconds since Epoch.
set saveToFileName to POSIX path of ¬
(path to pictures folder) & ¬
(do shell script "date +%s")
-- # Define the commands necessary to accomplish
-- # the download base on the OP's URL example.
set shellCMD to "curl -ks " & theURL's quoted form & " -o " & saveToFileName's quoted form & ¬
"; mv -n " & saveToFileName's quoted form & space & saveToFileName's quoted form & ¬
".$(file " & saveToFileName's quoted form & " | awk -F ': | ' '{print tolower($2)}')"
-- # Download the file.
do shell script shellCMD
注:示例 AppleScript
代码仅此而已,不包含任何错误处理。用户有责任添加任何适当的,需要的或想要的。看一下try中的error statement和AppleScript Language Guide statement。另请参阅Working with Errors。另外,在适当的情况下,例如,在事件之间,可能需要使用delay command。delay 0.5
,并且已适当设置delay的value。