如何在电子邮件中找到 link 并点击它? (Chrome / applescript / javascript)

How to find a link in an email and click on it? (Chrome / applescript / javascript)

我得到的部分outlook收件箱代码:

<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
<tbody><tr>
<td height="2" colspan="3" style="line-height:2px;">&nbsp;</td>
</tr>
<tr>
<td><**a href="https://www.facebook.com/confirmcontact.php?c=31085&amp;z=0&amp;gfid=AQALAirKRBiAw5qf1L4"** target="_blank" rel="noopener noreferrer" data-auth="NotApplicable" style="color:#3B5998;text-decoration:none;" data-linkindex="2">
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
<tbody><tr>
<td style="text-align:center;background-color:#4C649B;display:block;border-collapse:collapse;border-radius:2px;padding:7px 16px 11px 16px;border:1px solid #344C80;">
<a href="https://www.facebook.com/confirmcontact.php?c=31085&amp;z=0&amp;gfid=AQALAirKRBiAw5qf1L4" target="_blank" rel="noopener noreferrer" data-auth="NotApplicable" style="color:#3B5998;display:block;text-decoration:none;" data-linkindex="3">
<center><font size="3"><span style="color:white;font-size:14px;font-family:Helvetica Neue,Helvetica,Lucida Grande,tahoma,verdana,arial,sans-serif;font-weight:bold;vertical-align:middle;white-space:nowrap;line-height:14px;">Подтвердить</span></font></center>
</a></td>
</tr>
</tbody></table>
</a></td>
<td width="100%"></td>
</tr>
<tr>
<td height="32" colspan="3" style="line-height:32px;">&nbsp;</td>
</tr>
</tbody></table>
</span>

如何找到以 https://www.facebook.com/confirmcontact.php 开头的 link 并使用苹果脚本/javascript 单击它。 Chrome.

我试试这个,但还不是很喜欢。

tell application "Google Chrome"
    activate
    tell active tab of window 1
        execute javascript "document.querySelector('a[href=www.facebook.com/confirmcontact]').click()"
    end tell
end tell

也得到了建议,但有错误:错误:ReferenceError:找不到变量:文档

var links=document.getElementsByTagName('a');
for(var i=0; i<links.length;i++){
  if(links[i].match('www.facebook.com/confirmcontact')){
    links[i].click();
    break;//break the loop
  }
}

需要你的帮助,同事们。

使用 javascript,您可以使用以下查询在页面上找到所有 link:document.querySelectorAll("a"); or document.getElementsByTagName(a);。获得 links 后,您可以遍历 list/array 并使用 string1.match(string2);,其中 'string1' 是 link 在位置 i 的 href 属性数组和 'string2' 是您要查找的 href。检索到 link(匹配的那个)后,只需在其上使用 .click(); 即可触发点击。 它应该看起来像这样:

var links=document.getElementsByTagName('a');
for(var i=0; i<links.length;i++){
  if(links[i].match('www.facebook.com/confirmcontact')){
    links[i].click();
    break;//break the loop
  }
}

请记住,我不是 Javascript 方面的专家,但您的代码存在两个问题:

  1. 您没有引用 link
  2. document.querySelector(); returns 一个列表。您必须指定要定位的元素

最终产品看起来像这样:

tell application "Google Chrome"
    activate
    tell active tab of window 1
        execute javascript "document.querySelectorAll(\"a[href*='www.facebook.com/confirmcontact']\")[0].click();"
    end tell
end tell

上面发布的代码已在 Google Chrome 版本 92.0.4515.131(正式版)(x86_64)[=25 上的 MacBook Air 2019 上进行了测试=]