2013-02-23 157 views
1

我試圖讓每一個地方我訪問nike.com運動鞋頁面時,它會自動挑選我的鞋的大小,將其添加到購物車,並檢查了我。每次我嘗試運行腳本,我不斷收到此錯誤爲什麼我的Tampermonkey腳本拋出「Selenium沒有定義」?

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

這裏是我的腳本:

// ==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"); 

幫助非常感謝,謝謝!

編輯:

我只是跑它通過JSLint的,並得到了這個錯誤:

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

+0

您在使用前應定義「硒」。確保您的腳本在包含硒之後運行。 – jeroenvisser101 2013-02-23 15:51:46

回答

0

你從哪裏得到您試圖硒碼(selenium.select...,等等)?網頁本身是否使用Selenium? (疑)。

Tampermonkey不支持硒語法。你需要@require某種庫是的,我不知道這樣的庫的(但我不是一個專家硒)。

您需要使用JavaScript,或者庫,你@require,或功能是在目標頁面上發展Tampermonkey腳本。

這裏就是你的腳本可能是使用jQuerywaitForKeyElements庫/公用事業:

// ==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); 
} 


你將不得不調整使用HTML從實際網頁中的選擇(特別是第一個) ,你應該在問題中包含

+0

我只是試過你給我的關於篡改密碼的代碼,它沒有選擇鞋子,它只是讓我在我的購物車裏沒有任何東西結帳。 – Nite 2013-02-24 01:38:01

+0

這是因爲你沒有提供關於頁面的足夠信息(HTML代碼片段和鏈接是最低限度的)。無論如何,這與這個問題無關,我向你展示了爲什麼你遇到了錯誤以及如何解決這個問題。讓你的終極腳本工作不是問題的一部分(也不適合,Stack Overflow不是腳本編寫服務)。如果您需要進一步幫助,請回答這個問題,並用新問題創建一個新問題。可能是這樣的:「我如何選擇這種控件然後點擊這種按鈕?」 – 2013-02-24 01:57:41

+0

那麼,你是否爲價格創建腳本?我會有興趣付錢給你。 – Nite 2013-02-24 02:18:56

相關問題