TamperMonkey can't click button [duplicate]

半腔热情 提交于 2019-12-13 06:21:13

问题


This is the website I want to use TamperMonkey to click button. This is my script code:

// ==UserScript==
// @name         click
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://secure.timesheets.com/
// @include      https://secure.timesheets.com/default.cfm?page=Login&CFID=*
// @grant        none
// ==/UserScript==
start();
function start() {
    'use strict';
    var domd = document.getElementById('large-button-clockin');
    console.log(domd);
    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    async function demo() {
        console.log('Taking a break...');
        await sleep(10000);
        console.log('Ten second later');
        document.getElementById('large-button-clockin').click();
    }
    demo();
};

It shows:

<button class="largeButton loginButton" name="HourlyClockInButton" id="large-button-clockin" data-login-value="IN" type="button">HOURLY<div>CLOCK IN</div>CLICK HERE</button>
Taking a break...

And when the time arrives, it looks just like it refresh the page not click the button, and the CFID code change to another one. BTW, the browser would fill my form automatically.

But if I just put the code document.getElementById('large-button-clockin').click(); in the console of browser. It would click the code. But I can't do this in the TamperMonkey.

Do you know why?

Edit: I just found the reason, I need to fill the form in the script before I click the button. But the browser has already automatically do that for me. Do you know why I still need to do that in the script?

来源:https://stackoverflow.com/questions/50971986/tampermonkey-cant-click-button

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