Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simulate keyboard input / insert string into textarea (adwords)

I am trying to scrape keywords from https://adwords.google.com/ko/KeywordPlanner/Home using CefSharp (version 43)

I am able to log into the adwords portal programmatically.

However, whenever I try to insert some text (search keywords) into the specific textarea (id="gwt-debug-keywords-text-area") by assigning element.value, element.innerText and element.innerHtml to the desired string, but nothing happens.

var txtElem = document.getElementById("gwt-debug-keywords-text-area");
txtElem.addEventListener("keydown", onKeyPress, false);

function onKeyPress(e) {
    alert(e.keyCode);
}

function triggerKeyboardEvent(el, keyCode) {
    var evt = document.createEventObject ?
        document.createEventObject() : document.createEvent("Events");

    if (evt.initEvent) {
        evt.initEvent("keydown", true, true);
    }

    evt.keyCode = keyCode;
    evt.which = keyCode;

    el.dispatchEvent ? el.dispatchEvent(evt) : el.fireEvent("onkeydown", evt);
}

triggerKeyboardEvent(txtElem, "s".charCodeAt(0));

In the above code alert message is raised, yet the textbox still remains empty.

Apparently, key-up/down events on that textarea are being intercepted by google's own voluminous JS code. Unfortunately Googles sourcecode are highly obfuscated and it's very difficult for a JS-noob like me to debug & follow the logic.

How may I insert / simulate keyboard typing into the textarea?

Some other code I've tried (also see the commented out sections):

var txtElem = document.getElementById("gwt-debug-keywords-text-area");
txtElem.focus();

function simulateKeyEvent(character) {
    var e = new KeyboardEvent("keydown", { bubbles: true, cancelable: true, key: character.charCodeAt(0), char: character, shiftKey: false });
    //var e = new KeyboardEvent("keydown", { bubbles: true, cancelable: true, key: "a".charCodeAt(0), char: "a", shiftKey: true });
    return !document.getElementById("gwt-debug-keywords-text-area").dispatchEvent(e);

    var evt = document.createEvent("KeyboardEvent");
    /*
    if (e.initKeyboardEvent) {  // Chrome, IE
        e.initKeyboardEvent("keydown", true, true, document.defaultView, "Enter", 0, "", false, "");
    } else { // FF
        e.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, character.charCodeAt(0), 0);
    }
    */
    //(evt.initKeyEvent || evt.initKeyboardEvent)('keypress', true, true, document.defaultView, 0, 0, 0, 0, 0, character.charCodeAt(0));
    evt.initKeyboardEvent("keypress", true, true, document.defaultView, 0, 0, 0, 0, 0, character.charCodeAt(0));
    return !document.getElementById("gwt-debug-keywords-text-area").dispatchEvent(evt);
}

simulateKeyEvent("s");

None of these methods work....

like image 738
masroore Avatar asked Nov 29 '22 01:11

masroore


1 Answers

Ok, I found the solution:

function setKeywordText(text) {
    var el = document.getElementById("gwt-debug-keywords-text-area");
    el.value = text;
    var evt = document.createEvent("Events");
    evt.initEvent("change", true, true);
    el.dispatchEvent(evt);
}

setKeywordText("test");
like image 174
masroore Avatar answered Feb 04 '23 00:02

masroore