Javascript 如何自动点击网页上的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27539804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:17:25  来源:igfitidea点击:

How to Automatically Click Button on a Webpage

javascriptjquery

提问by Ivan

I'm completely new to scripting and i'm having an awful time trying to create a script so that it automatically clicks an "Add to Cart" button on the webpage once I visit it. An example on the website is the item "https://www.therealreal.com/products/women/outerwear/Hymanets/chanel-Hymanet-931420". Ive tried using the following script

我对脚本编写完全陌生,我在尝试创建脚本时遇到了很糟糕的时间,以便在我访问网页时它会自动单击网页上的“添加到购物车”按钮。网站上的一个例子是项目“ https://www.therealreal.com/products/women/outerwear/Hymanets/chanel-Hymanet-931420”。我试过使用以下脚本

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js" 
 type="text/javascript">/script> 
<script type="text/javascript">
 $(function(){    
    setTimeout(function() {
       $("add-to-cart-button").trigger('click');
    },1);
 });
</script>

But there was no luck. If someone could help me create a script that would automatically click the add to cart button once I visit any item on the website I would be extremely grateful and would even be willing to pay for this script. Oh also im on a mac if it makes a difference.

但是没有运气。如果有人可以帮助我创建一个脚本,当我访问网站上的任何项目时,该脚本会自动单击“添加到购物车”按钮,我将非常感激,甚至愿意为此脚本付费。哦,如果它有所作为,我也在 mac 上。

回答by Mangesh Parte

Try this:

尝试这个:

If add-to-cart-buttonis ID:

如果add-to-cart-button是身:

$("#add-to-cart-button").trigger('click');

If add-to-cart-buttonis Class:

如果add-to-cart-button是类:

$(".add-to-cart-button").trigger('click');

回答by Ahmed Walaa Eldin

i think if you follow the idea that i will show, you will catch the solution

我想如果你遵循我将展示的想法,你就会找到解决方案

consider that you have two buttons one called "Call set time out" and another is your desired button "Add to cart"

考虑到您有两个按钮,一个称为“呼叫设置超时”,另一个是您想要的按钮“添加到购物车”

and on "Call set time out" click event you want to call "Add to Cart" button event so you should do the following

并在“呼叫设置超时”单击事件上要调用“添加到购物车”按钮事件,因此您应该执行以下操作

the html will be

html 将是

<input type="button" id = "Call-set-time-out" value= "Call set time out" />
<input type="button" id = "add-to-cart-button" value= "Add to cart"/>

and the jquery script will be

和 jquery 脚本将是

 $( "#Call-set-time-out" ).click(function() {
       $("#add-to-cart-button").trigger('click');
    });

$( "#add-to-cart-button" ).click(function() {
  alert( "add-to-cart-button .click() event called." );
});

i hope this will help you and good luck for you journey in Jquery world

我希望这会帮助你并祝你在 Jquery 世界中的旅程好运

回答by Nikhil Chavan

I think $("#add-to-cart-button").click(); will work for you based on if add-to-cart-button is id of that button.

我认为 $("#add-to-cart-button").click(); 将根据添加到购物车按钮是否为该按钮的 id 为您工作。

回答by ManoharSingh

Hi try the below code this should help

嗨,试试下面的代码,这应该会有所帮助

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".add-to-cart-button").trigger('click');
});
</script>

Happy Coding :)

快乐编码:)