0

Actually, I am working on an extension which help to buy product from flipkart during flash sale. On teh product page I can't click on "Buy Now" button with the help of JavaScript its show me undefined this is the code of Buy Now button:

<form><button class="_2AkmmA _2Npkh4 _2kuvG8 _7UHT_c" type="button"><span class="_279WdV"></span> <!-- -->BUY NOW</button></form> 

The Button is in this page

I am using the code to click this button in my JavaScript file

$("._2AkmmA._2Npkh4._2kuvG8._7UHT_c").click(); 

However, even when I go to the page on the site and, in the console, execute

$("._2AkmmA._2Npkh4._2kuvG8._7UHT_c").click(); 

The console shows undefined and does not start the "Buy Now" process that is started by manually using the mouse to click on the "Buy Now" button. In the console, executing

$("._2AkmmA._2Npkh4._2kuvG8._7UHT_c"); 

Shows:

<button class="_2AkmmA _2Npkh4 _2kuvG8 _7UHT_c" type="button"><span class="_279WdV"></span> <!-- -->BUY NOW</button> 

which is the correct button that I desire to programmatically click. Note that even though I used $(), what is returned is not a jQuery Object.

If I click on that button manually, I'm shown the dialog for "Buy Now". You can go to this page and try it yourself.

  • I am not getting what is the problem here. Is there any problem in my code or its any kind of protection form filpkart website side?
14
  • Your code works as expected. Commented Dec 13, 2018 at 7:19
  • Your code is okay, it might be an issue for document loading. Try to paste the code in console and try. You might need a settimeout/interval... Commented Dec 13, 2018 at 7:32
  • @Makyen wow very good thansks for increaseing my information about stackoverflow. And you rely don't have sence to read the question please read carefully before putting any comments I have clearly mentioned that button is not working by clicking using jQuery on the given website link . I don't think so it's related to manifest.jons file Commented Dec 14, 2018 at 19:43
  • 1
    Really strange. Anyway when I tried to get the function on the button, I get codes starting as return o.handleBuyNow=function(e){var t=o.props,n=t.wiggleSwatch,r=t.userState,i=t.data,s=t.location;if(e&&!a.i(E.f)(e))return!1;if("function"==typeof n)return n();o.setState({isBuying:!0});var c=h()(i,"action"),l=h()(s,"query.otracker"),u=c.params||{},d={products:y.a.getValueFromCurrentContext("products")};. Use $("._2AkmmA._2Npkh4._2kuvG8._7UHT_c").__reactEventHandlers$pvrhr6cl6gh;, as you type the . after ), you should see it in Chrome. Looks like React.js, Update your tag to React JS. Commented Dec 21, 2018 at 19:51
  • 1
    any answer for this question ? Commented Mar 14, 2019 at 7:21

3 Answers 3

-1

I think you must use multi class selector in jQuery like this:

$("._2AkmmA, ._2Npkh4, ._2kuvG8, _7UHT_c").click() 

As you see I use , between class or reduce your class like this :

$("._2AkmmA").click() 
Sign up to request clarification or add additional context in comments.

1 Comment

@connexo why? for the second code I can agree with you but for the multi class selector I can't.
-1

You have to call the code only when All elements are full loaded, otherwise it won't find it. You must wrap in JQuery like :

$(document).ready(function(){ $("._2AkmmA._2Npkh4._2kuvG8._7UHT_c").click(); }); 

Comments

-1

Your code is probably running before the DOM is fully loaded.

Try wrapping your code with $(document).ready(function(){... which will ensure that the code inside will only run once the page Document Object Model (DOM) is ready.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("._2AkmmA._2Npkh4._2kuvG8._7UHT_c").click(); }); function test(){ alert('clicked'); } </script> <form><button class="_2AkmmA _2Npkh4 _2kuvG8 _7UHT_c" type="button" onclick="test()"><span class="_279WdV"></span> <!-- -->BUY NOW</button></form>

7 Comments

How can anyone add the onclik function on the button where he is trying to wright and extension?
@pixellab The OP didn't mention the reason he wants to click the button programmatically. OnClick doesn't seem to be an option for him on this case.
@Vishalchoudhary, then you should create a live example of the problem.
@GiulioBambini actually the live example mention there, he wants to click the buy now button on the above link.
@pixellab, I mean to say here in the SO or in a fiddle.....not in that link.....thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.