If a button appears on a page, automatically click it

邮差的信 提交于 2019-12-23 05:28:03

问题


<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. 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)

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

Thank you!


回答1:


Updated: Now checks contents of element

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

Have a look at the querySelector and textContent for more information

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();


来源:https://stackoverflow.com/questions/16191804/if-a-button-appears-on-a-page-automatically-click-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!