【问题标题】:How to find a link in an email and click on it? (Chrome / applescript / javascript)如何在电子邮件中找到链接并单击它? (铬/苹果脚本/javascript)
【发布时间】:2021-10-18 19:02:55
【问题描述】:

我收到的 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 开头的链接并使用苹果脚本/javascript 来点击它。铬。

我尝试过这个,但还不是很多。

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

也得到了建议,但出现了错误:Error: ReferenceError: Can't find variable: document

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
  }
}

需要你的帮助,同事们。

【问题讨论】:

  • 您提供的代码无法编译,您在 facebook 链接周围的引号不应加倍。
  • 谢谢。我修好了它。但不起作用。(

标签: javascript google-chrome hyperlink jquery-selectors applescript


【解决方案1】:

使用 javascript,您可以使用查询:document.querySelectorAll("a"); or document.getElementsByTagName(a); 找到页面上的所有链接。获得链接后,您可以遍历列表/数组并使用string1.match(string2);,其中“string1”是数组位置 i 处链接的 href 属性,“string2”是您要查找的 href。检索到链接(匹配的链接)后,只需在其上使用 .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
  }
}

【讨论】:

  • 在applescript“conteiner”的自动机中,我把我的部分代码:告诉应用程序“Google Chrome”激活结束告诉延迟1,我把你的代码放在自动机javascript“conteiner”中。因为我不知道如何在 javascript 中激活当前的 chrome 标签。并且出现错误:“运行 JavaScript”操作遇到错误:“错误:ReferenceError:找不到变量:文档”。请你完善你的代码吗?
【解决方案2】:

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

  1. 你没有引用链接
  2. document.querySelector(); 返回一个列表。您必须指定要定位的元素

最终产品看起来像这样:

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

上面发布的代码在 MacBook Air 2019 上使用 Google Chrome 版本 92.0.4515.131(官方构建)(x86_64) 进行了测试

【讨论】:

  • gurkensaas,感谢您的建议。请将 ...href*='ww... 添加到您的代码中。一切都会奏效。我会解决的。再次感谢!
  • @Bro 添加星星对我没有任何帮助。我也找不到关于星星的任何文档,但它不会破坏代码。如果你能给我解释一下,我很乐意添加它。
  • = 完全相等 != 不相等 ^= 以 $= 开头 以 *= 结尾 包含 ~= 包含单词
  • @Bro 我修好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 2016-06-24
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多