简体   繁体   English

用javascript单击页面上的所有按钮

[英]Clicking all the buttons on the page with javascript

So I am trying to click all the buttons on a specific webpage. 因此,我尝试单击特定网页上的所有按钮。 But the tricky thing is that the buttons only show up when a mouse hovers over them. 但是棘手的是,只有当鼠标悬停在按钮上方时,按钮才会显示。 After I finish clicking all the buttons on the page, I need to click the next page button. 单击页面上的所有按钮后,需要单击下一页按钮。 And again click all the buttons on the new webpage and so on. 然后再次单击新网页上的所有按钮,依此类推。

I actually have a working script. 我实际上有一个工作脚本。 But I am using setInterval function in javascript and I feel like it is a hacky way to do it. 但是我在javascript中使用setInterval函数,我觉得这是一种很怪异的方法。 This is my working script: 这是我的工作脚本:

alert ("Please refresh the page to stop");

setInterval(function () {

    var inputs=document.getElementsByClassName('tw-action-button btn btn-sm ng-isolate-scope btn-primary');
    var names=document.getElementsByClassName('screen-name');       

    for(var i=0; i<inputs.length; i++){
        inputs[i].click();      
    }       

}, 250);

//Change the page in every 15 seconds   
setInterval(function () {

var nextButton=document.getElementsByClassName('btn btn-default btn-forward');

    for(var i=0; i<nextButton.length; i++){
        nextButton[i].click();
    }

    window.scrollTo(0,0);

}, 10000);

However when I change the above code to the one below, the script doesn't click the buttons. 但是,当我将上面的代码更改为下面的代码时,脚本不会单击按钮。 Do I have to use the setInterval function? 我必须使用setInterval函数吗? This is the modified but not working code: 这是修改后的但不起作用的代码:

function clickButtons(){

        var followButtons = $('.buttonClass');
        var numberOfButtons = followButtons.length;

        for(var i=0; i<numberOfButtons; i++){
            followButtons[i].click();
        }
        setTimeout(nextPage, 500);
    }

    function nextPage(){

        var nextPageBtn = $('.nextPageButtonClass');
        nextPageBtn.click();    

        setTimeout(clickButtons,500);
    }

This is the HTML for the webpage: 这是网页的HTML: 网页的HTML

I'd probably consider using a bookmarklet or if the project warranted it, a browser extension. 我可能会考虑使用书签,或者,如果项目需要,可以使用浏览器扩展。

Since your code needs to apply the same action to a number of pages, you somehow need to get access to the HTML of each of them, all the while retaining access to your JS code. 由于您的代码需要对多个页面应用相同的操作,因此您需要以某种方式访问​​每个页面的HTML,同时始终保持对JS代码的访问。

In the case of a bookmarklet, each time you click on it, you could process all buttons on the page, before clicking the button that advances to the next page. 对于书签,每次单击书签时,您都可以处理页面上的所有按钮,然后单击前进到下一页的按钮。 At this point, you'd just click the bookmarklet again. 此时,您只需再次单击小书签。

In the case of an extension, I'd use a boolean value that indicated whether or not I wanted to perform the specified actions each time I visited a page from the owning domain. 对于扩展,我将使用一个布尔值,该值指示每次我从拥有的域访问页面时是否要执行指定的操作。 Then, once enabled, I'd just visit the first page and let it click all of the buttons, click the one that advances and allow the script to repeatedly be invoked. 然后,一旦启用,我将访问第一页,让它单击所有按钮,单击前进的按钮,并允许重复调用脚本。

I imagine that a bookmarklet would be the most suitable option of these two for this - quick, easy, few edge-cases to watch out for. 我认为小书签将是这两者中最合适的选择-快速,容易,需要注意的边缘情况。

I have a bookmarklet that allows you to make any table on a page sortable. 我有一个小书签,可让您对页面上的任何表格进行排序。 One simply navigates to the page, clicks the bookmarklet and then voila! 只需导航到页面,单击书签,然后瞧瞧! Clicking on a column in the header row sorts the table based on that row. 单击标题行中的一列将根据该行对表进行排序。 It's example code supplied by the sorttable js library ( http://www.kryogenix.org/code/browser/sorttable/#ajaxtables ) 这是sorttable js库( http://www.kryogenix.org/code/browser/sorttable/#ajaxtables )提供的示例代码

Here's the bookmarklet's 'URL': javascript:(function(){var s=document.createElement('script');s.src='http://kryogenix.org/code/browser/sorttable/sorttable.js';s.onload=function(){sorttable.init();Array.prototype.slice.call(document.getElementsByTagName('table')).forEach(function(t){sorttable.makeSortable(t);})};document.getElementsByTagName('head')[0].appendChild(s);})() 这是书签的“ URL”: javascript:(function(){var s=document.createElement('script');s.src='http://kryogenix.org/code/browser/sorttable/sorttable.js';s.onload=function(){sorttable.init();Array.prototype.slice.call(document.getElementsByTagName('table')).forEach(function(t){sorttable.makeSortable(t);})};document.getElementsByTagName('head')[0].appendChild(s);})()

在此处输入图片说明

It seems to me as though it would be trivial to inset some code to perform the button-clicking into one of these. 在我看来,插入一些代码来执行按钮单击其中之一似乎微不足道。

That's strange, it should be working. 奇怪,它应该可以正常工作。 I tried to produce the same logic as you did, and it worked successfully, here's my code : 我试图产生与您相同的逻辑,并且成功运行,这是我的代码:

<!DOCTYPE html>
<HTML>
<HEAD>
</HEAD>
<BODY>
    <button type="button" onclick="doAction()" class="tw-action-button btn btn-sm ng-isolate-scope btn-primary"></button>
    <button type="button" onclick="doAction()" class="tw-action-button btn btn-sm ng-isolate-scope btn-primary"></button>
    <script type="text/javascript">
        function doAction(){
            alert("I'm clicked");
        }
        function clickButtons(){
            var inputs = document.getElementsByClassName("tw-action-button btn btn-sm ng-isolate-scope btn-primary");
            for (var index=0;index < inputs.length; index++){
                inputs[index].click();
            }
        }
        setTimeout(clickButtons,500);
    </script>
</BODY>
</HTML>

Then when I look again at your code, it looks like your JQuery was not loaded successfully, since you use selector, it's the only possibility I can think of. 然后,当我再次查看您的代码时,您的JQuery似乎未成功加载,因为您使用选择器,这是我能想到的唯一可能性。

var followButtons = $('.buttonClass');

To make sure it's loaded successfully, try to replace all of your javascript scripts, with this one, and see if it pops up an alert message : 为确保已成功加载,请尝试用此脚本替换所有JavaScript脚本,然后查看它是否弹出警告消息:

<script type="text/javascript">
    $(document).ready(function(){
        alert("JQuery loaded");
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM