如果页面上出现按钮,则会自动单击该按钮 [英] If a button appears on a page, automatically click it

查看:109
本文介绍了如果页面上出现按钮,则会自动单击该按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<a href="javascript:void(0)" class="PrmryBtnMed"
 id = "VERYLONGTEXT"

onclick="$(this).parents('form').submit(); return false;"><span>Dispatch to this address</span></a>

如果我要向人类发出指示,我会说:

If I was to give instructions to a human, I would say:


  1. 寻找< span>发送到此地址< / span> 链接;如果出现点击它。 (如果有多个这样的链接(例如有两个)只需点击第一个(或任何一个)

  1. look for a <span>Dispatch to this address</span> link; if it appears click it. (if there is more than one such link (for example there are two) just click the first one (or any of them)

我正在使用Greasekit,所以我希望使用JavaScript。

I am using Greasekit, so I am looking to do this using JavaScript.

谢谢!

推荐答案

更新:现在检查元素的内容

var el = document.querySelector(".PrmryBtnMed"); //should only return first one
if (el && (el.textContent.trim() == 'Dispatch to this address')) {
  el.click();
} 

看看 querySelector textContent 了解更多信息

我还为你添加了一个JsFiddle: http://jsfiddle.net/NrGVq/1/

I also added a JsFiddle for you: http://jsfiddle.net/NrGVq/1/

另一种方法是搜索整个页面,以防它不在匹配元素内

A different approach is to search the whole page, in case it's not inside the matched element

var inPage = document.documentElement.innerHTML.indexOf('text to search') > 0,
    el = document.querySelector(".PrmryBtnMed");

if (inPage && el) el.click();

这篇关于如果页面上出现按钮,则会自动单击该按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆