获取 applescript 中 javascript 操作返回的文本
Get text returned of javascript action in applescript
所以我想要一个脚本来为我按下按钮,如果按钮不存在,它会一直点击直到按钮存在。我尝试的方法是获取我的 JavaScript 操作的 text returned
,因为如果我尝试按下的按钮不存在,它通常会将 missing value
写入结果选项卡脚本编辑器。该方法不起作用,因为下面的代码甚至无法编译。
repeat until text returned of theAction is not equal to "missing value"
tell application "Google Chrome"
tell active tab of front window to set theAction to execute javascript "document.getElementById('getQualities').click();"
end tell
end repeat
So I want to have a script that presses a button for me and if the button doesn't exist, it keeps clicking until the button exists.
这是我会使用并且应该有效的方法。我说 应该 因为我无法基于 元素 是 getQualities
进行测试。但是,我确实在这个问题的页面上使用已知的 元素 测试了 code 并且它有效。
示例 AppleScript 代码:
set theElementExists to false
repeat until theElementExists
tell application "Google Chrome" to ¬
set theElementExists to ¬
execute front window's active tab javascript ¬
"document.body.contains(document.getElementById('getQualities'));"
delay 1
end repeat
if theElementExists then
tell application "Google Chrome" to ¬
execute front window's active tab javascript ¬
"document.getElementById('getQualities'}.click();"
end if
备注:
我不太喜欢使用无限循环结构,但当我这样做时,我通常会添加额外的 code 来终止 script 经过一段合理的超时时间。 (这当然不会让它们成为无限循环。)
示例 AppleScript 代码:
set theElementExists to false
set i to 0
repeat until theElementExists
tell application "Google Chrome" to ¬
set theElementExists to ¬
execute front window's active tab javascript ¬
"document.body.contains(document.getElementById('getQualities'));"
delay 1
set i to i + 1
if i ≥ 10 then return
end repeat
按照编码,这将 运行 最多 10 秒。
document.body.contains()
方法returns一个布尔值.
请注意,第二个 if
语句 if theElementExists then
本身并不是必需的,我将其包括在内主要是为了编码清晰。
如果您想知道我是如何测试它的,我在我的代码中使用了以下内容,它针对提问[=此页面上的 41=] 按钮:
"document.body.contains(document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0]);"
并且:
"document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();"
先手动将焦点设置到另一个页面,然后手动将焦点设置回此页面,然后单击 提问 按钮 .
所以我想要一个脚本来为我按下按钮,如果按钮不存在,它会一直点击直到按钮存在。我尝试的方法是获取我的 JavaScript 操作的 text returned
,因为如果我尝试按下的按钮不存在,它通常会将 missing value
写入结果选项卡脚本编辑器。该方法不起作用,因为下面的代码甚至无法编译。
repeat until text returned of theAction is not equal to "missing value"
tell application "Google Chrome"
tell active tab of front window to set theAction to execute javascript "document.getElementById('getQualities').click();"
end tell
end repeat
So I want to have a script that presses a button for me and if the button doesn't exist, it keeps clicking until the button exists.
这是我会使用并且应该有效的方法。我说 应该 因为我无法基于 元素 是 getQualities
进行测试。但是,我确实在这个问题的页面上使用已知的 元素 测试了 code 并且它有效。
示例 AppleScript 代码:
set theElementExists to false
repeat until theElementExists
tell application "Google Chrome" to ¬
set theElementExists to ¬
execute front window's active tab javascript ¬
"document.body.contains(document.getElementById('getQualities'));"
delay 1
end repeat
if theElementExists then
tell application "Google Chrome" to ¬
execute front window's active tab javascript ¬
"document.getElementById('getQualities'}.click();"
end if
备注:
我不太喜欢使用无限循环结构,但当我这样做时,我通常会添加额外的 code 来终止 script 经过一段合理的超时时间。 (这当然不会让它们成为无限循环。)
示例 AppleScript 代码:
set theElementExists to false
set i to 0
repeat until theElementExists
tell application "Google Chrome" to ¬
set theElementExists to ¬
execute front window's active tab javascript ¬
"document.body.contains(document.getElementById('getQualities'));"
delay 1
set i to i + 1
if i ≥ 10 then return
end repeat
按照编码,这将 运行 最多 10 秒。
document.body.contains()
方法returns一个布尔值.
请注意,第二个 if
语句 if theElementExists then
本身并不是必需的,我将其包括在内主要是为了编码清晰。
如果您想知道我是如何测试它的,我在我的代码中使用了以下内容,它针对提问[=此页面上的 41=] 按钮:
"document.body.contains(document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0]);"
并且:
"document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();"
先手动将焦点设置到另一个页面,然后手动将焦点设置回此页面,然后单击 提问 按钮 .