Why does my Tampermonkey script throw “Selenium is not defined”?

£可爱£侵袭症+ 提交于 2019-12-06 07:19:06

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!