2

I am trying to make it where every time I visit a nike.com sneaker page, it automatically picks my shoe size, adds it to the cart, and checks out for me. Every time I try to run the script, I keep getting this error

ERROR: Execution of script 'My Fancy New Userscript' failed! selenium is not defined

Here is my script:

// ==UserScript==
// @name       My Fancy New Userscript
// @namespace  http://*/*
// @version    0.1
// @description  enter something useful
// @match      http://*/*
// @copyright  2012+, You
// ==/UserScript==

selenium.select("class=selectBox-label", "10"); // this selects size 10 
selenium.click("class=add-to-cart nike-button nike-button-orange");
selenium.waitForElement("class=checkout-button nike-button nike-button-orange"); 
selenium.click("class=checkout-button nike-button nike-button-orange");

Help is very much appreciated, thank you!

Edit:

I just ran it through JSLint, and got this error:

'selenium' was used before it was defined. (Line 1 Character 1) ----> selenium.select("class=selectBox-label", "10"); // this selects size 10

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Nite
  • 268
  • 1
  • 4
  • 12

1 Answers1

1

Where did you get that Selenium code (selenium.select..., etc.) that you are trying? Does the web page itself use Selenium? (Doubtful).

Tampermonkey does not support Selenium syntax. You'd need to @require some kind of library for that, and I'm not aware of such a library (but I'm not a Selenium expert).

You need to use javascript, or libraries that you @require, or functions that are on the target page to develop Tampermonkey scripts.

Here's what your script might be using the jQuery and waitForKeyElements libraries/utilities:

// ==UserScript==
// @name     _Nike auto-buy(!!!) script
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

var okayToClickAddtoCart = false;

//-- Assumes that size is a standard <option> tag or similar...
waitForKeyElements (".selectBox-label[value='10']", selectShoeSize);

function selectShoeSize (jNode) {
    jNode.prop ('selected', true);

    okayToClickAddtoCart = true;
}


waitForKeyElements (".add-to-cart.nike-button", clickAddToCart);

function clickAddToCart (jNode) {
    if ( ! okayToClickAddtoCart) {
        return true;    //-- Don't click yet.
    }

    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}


waitForKeyElements (".checkout-button", clickCheckoutButton);

function clickCheckoutButton (jNode) {
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}


You will have to tune the selectors (especially the first one) using the HTML from the actual page, which you should include in the question.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • I just tried that code you gave me on tampermonkey, and it doesn't pick the shoes, it just takes me to checkout with nothing in my cart. – Nite Feb 24 '13 at 01:38
  • That's because you didn't provide adequate information about the page (HTML snippets and a link are the bare minimum). It's irrelevant to this question anyway, I showed why you got the error and how to fix it. Making your ultimate script work was not part of the question (nor would it be appropriate, Stack Overflow is not a script writing service). If you want further help, mark this question answered and create a new question with the new issue. Maybe something like: "How do I select this kind of control and then click this kind of button?" – Brock Adams Feb 24 '13 at 01:57
  • Well, do you create scripts for a price? I would be interested in paying you. – Nite Feb 24 '13 at 02:18
  • I don't do freelance work, but you could easily get someone to do this at one of the many freelance sites. As I understand your situation, you basically now just need help selecting and activating the correct controls. ... My answer code does that kind of thing, but it needs to be tuned for the actual page, and no one can do that, paid or not, without the exact HTML and/or a link to the exact page in question, or a pastebin snapshot of the page's HTML. My price for further help is that you mark this answered and create a new question with the needed information. – Brock Adams Feb 24 '13 at 02:27
  • The Selenium *problem* /question is solved. Your *project* now has a *new* problem -- choosing and activating the right controls. – Brock Adams Feb 24 '13 at 02:28
  • So, it is impossible to go to any Nike sneaker page and make it pick size and then checkout w/o the HTML link? – Nite Feb 24 '13 at 02:56
  • No that's not impossible. We need the link to know how to pick the size and add the shoes to the cart. (We need the jQuery selectors for these elements.) Such things are **not standard** and are ***extremely*** dependent on the exact page. If you have no lasting link you can save the page from Firefox and put the saved HTML up on http://pastebin.com/. My answer code will work, but the parameters passed to `waitForKeyElements` need to be tuned to the real page. – Brock Adams Feb 24 '13 at 03:01
  • Here is the new question link: http://stackoverflow.com/questions/15048223/choosing-and-activating-the-right-controls Is there anyway you can comment over there? Also, I am very new to programming, so I don't understand the lingo that well. What do I need to get you? I don't really understand. – Nite Feb 24 '13 at 03:10