【问题标题】:How to Automatically click Follow button with JavaScript script on page Console如何在页面控制台上使用 JavaScript 脚本自动单击“关注”按钮
【发布时间】:2018-04-24 12:22:37
【问题描述】:

总而言之,我一直在尝试在 StackOverflow 上找到的多个不同代码,但似乎都没有工作。

我的问题:

我希望能够使用 Javascript 代码自动单击网站上的按钮。

代码如下:

HTML:

<label class="button slim hollow secondary ">Follow</label>

JS(我正在尝试使用它但它不起作用):

document.getElementsByClassName("button slim hollow secondary ")[0].click();

当我使用控制台在页面上运行代码时它不起作用,有什么建议吗?

【问题讨论】:

  • 你收到错误了吗?
  • “未定义”是唯一的错误
  • "TypeError: document.getElementsByClassName(...)[0] is undefined"?
  • 控制台字面上只说未定义

标签: javascript jquery html button automation


【解决方案1】:

添加window.onload函数使窗口加载后点击,即html创建时

window.onload = clicklabel();


function clicklabel(){
document.getElementsByClassName("button slim hollow secondary ")[0].click();
document.getElementsByClassName("button slim hollow secondary ")[0].style.color = 'red';
}
<label class="button slim hollow secondary ">Follow</label>
<br>
<label class="button slim hollow secondary ">Follow1</label>
<br>
<label class="button slim hollow secondary ">Follow2</label>
<br>
<label class="button slim hollow secondary ">Follow3</label>
<br>
<label class="button slim hollow secondary ">Follow4</label>
<br>
<label class="button slim hollow secondary ">Follow5</label>
<br>
<label class="button slim hollow secondary ">Follow6</label>

【讨论】:

    【解决方案2】:

    你有多个类..你可以把词类当作属性
    `

    var btn = document.querySelectorAll('[class="button slim hollow secondary "]')[0];
    btn.click();
    

    `

    【讨论】:

      【解决方案3】:

      您可以使用document.querySelector(".class1.class2") 方法。

      window.onload = doSomething;
      function doSomething() {
          var btn = document.querySelector(".button.slim.hollow.secondary");
        btn.addEventListener("click", function(){
            console.log(this.innerHTML);
        });
        btn.click();
      }
      

      在控制台中,您将获得“关注”作为结果。

      当网页加载完毕(包括图片)后,window.onload 会调用我们的 doSomething 函数。

      【讨论】:

        猜你喜欢
        • 2021-08-05
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多