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?
Vishal choudhary
  • 315
  • 4
  • 10
  • Your code works as expected. – connexo Dec 13 '18 at 07: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... – pixellab Dec 13 '18 at 07: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 – Vishal choudhary Dec 14 '18 at 19:43
  • @Vishalchoudhary With a Chrome extension, without seeing that you are, in fact, injecting your code into the page as a content script, there's no way for us to know if your code is even running. Your description is unclear that you are actually manually going to the page which you've linked and in the console executing `$("._2AkmmA._2Npkh4._2kuvG8._7UHT_c").click();`, which does nothing and returns the result `undefined`. If you had clearly stated that, then what your issue is, or at least where to focus investigation, would have been significantly more clear. – Makyen Dec 14 '18 at 20:19
  • Note that a significant issue is that you appear to be assuming the page is running jQuery, which it is not. In addition, it appears the site is performing some checks on the `click` event. You will need to reverse engineer those checks and determine what's necessary to register a successful click, or you will need to determine what is being done by the click handler and duplicate those actions. – Makyen Dec 14 '18 at 20:39
  • In this case, in the console, when executing `$()` it uses the console API, not jQuery. Thus, your content script should produce an exception when you tried to do `$("._2AkmmA._2Npkh4._2kuvG8._7UHT_c").click();`. That you didn't report that exception here implies you are not looking where you should be looking for your content script errors, your content script is not actually loading, or you are loading jQuery yourself, in addition to your content script, which is something you really should have told us (all of these possibilities are some of the reasons I asked for a manifest.json). – Makyen Dec 14 '18 at 20:59
  • @Makyen ohhh rely its not a big issue of jQuery. Yes u can't include jQuery becouse of some security reasons but i think you can run jQuery code inside console. Okk download jQuery file open file copy all code and paste inside browser console press enter now you will be able to use jQuery function. Okkkkk i think Now i have cleared everything to u, waiting for your answer. – Vishal choudhary Dec 15 '18 at 19:55
  • It's likely that the event you need to generate must return `true` when the event is passed to the following function: `function u(e) {return Boolean(!(!1 === e.isTrusted) && (e.screenX && 0 !== e.screenX && e.screenY && 0 !== e.screenY || e.clientX && 0 !== e.clientX && e.clientY && 0 !== e.clientY))}`. However, I really haven't taken a close look at it, so that's just a guess. – Makyen Dec 21 '18 at 03:13
  • Maybe the the web page you point to has not registered `onClick()` event for the button, but for a parent element, and when this click occurs on the parent and check if it's the button with X and Y axis like old C Winbgim graphics. – KeitelDOG Dec 21 '18 at 18:04
  • @KeitelDog but its work when i press by keyboard also – Vishal choudhary Dec 21 '18 at 18:40
  • 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. – KeitelDOG Dec 21 '18 at 19:51
  • Are you using React JS? I saw this, maybe it could be interesting. https://stackoverflow.com/questions/39913863/how-to-manually-trigger-click-event-in-reactjs – KeitelDOG Dec 21 '18 at 19:58
  • 1
    any answer for this question ? – rocky Mar 14 '19 at 07:21
  • @rocky I didn't get the right ans yet can you help – Vishal choudhary Mar 16 '19 at 10:44

3 Answers3

-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()
MBehtemam
  • 7,865
  • 15
  • 66
  • 108
-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>
Giulio Bambini
  • 4,695
  • 4
  • 21
  • 36
  • How can anyone add the onclik function on the button where he is trying to wright and extension? – pixellab Dec 13 '18 at 07:27
  • 1
    @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. – KeitelDOG Dec 13 '18 at 07:34
  • @Vishalchoudhary, then you should create a live example of the problem. – Giulio Bambini Dec 13 '18 at 08:01
  • @GiulioBambini actually the live example mention there, he wants to click the buy now button on the above link. – pixellab Dec 13 '18 at 08:37
  • @pixellab, I mean to say here in the SO or in a fiddle.....not in that link.....thanks – Giulio Bambini Dec 13 '18 at 08:38
  • @GiulioBambini goto the [This link ](https://www.flipkart.com/espoir-es2615-day-date-functioning-high-quality-watch-men/p/itmf7vvspssz5vqz?pid=WATF7VKVBHSS7V5D&lid=LSTWATF7VKVBHSS7V5DTYTUOT&marketplace=FLIPKART&srno=b_1_1&otracker=hp_omu_Deals%2Bof%2Bthe%2BDay_1_Min.60%2525%2B%252B%2BExtra10%2525Off_NFJ7P9838BD1_0&fm=neo%2Fmerchandising&iid=5d4a093a-36c3-482a-a804-f90907c3b096.WATF7VKVBHSS7V5D.SEARCH&ppt=StoreBrowse&ppn=Store&ssid=ci4ysiaggw0000001544683181532). goto console and this is javascript code i used document.getElementsByClassName("_2AkmmA _2Npkh4 _2kuvG8 _7UHT_c")[0].click(); – Vishal choudhary Dec 13 '18 at 08:45
  • Welcome @GiulioBambini, I understand your point but how anyone can create a replica of a website on a fiddle or SO and his question based on that particular website link. – pixellab Dec 13 '18 at 08:47
-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();
});
KeitelDOG
  • 4,750
  • 4
  • 18
  • 33