I am trying to automate few tasks on some websites, including Google Finance. But, when I try to edit the input value in Google Finance Search Input, it simply doesn’t work. Is there anyone who can help me out with this?
I have already tried the following code in console:
element = document.querySelector("input[class='yNVtPc ZAGvjd Ny5lGc']")
element.value = "RELIANCE"
element2 = document.querySelector("input[class='Ax4B8 ZAGvjd']")
element2.value = "RELIANCE"
I even tried removing disabled attribute and changing default value, still no luck.
Furthermore, I tried the following function as well:
const sendKeys = async (el, str) => {
let val = "";
const keys = str.split("");
for (const key of keys) {
console.log(key);
val += key;
el.dispatchEvent(
new KeyboardEvent("keydown", { bubbles: true, cancelable: true, key })
);
el.dispatchEvent(
new KeyboardEvent("keypress", { bubbles: true, cancelable: true, key })
);
el.value = val;
el.dispatchEvent(new Event("input", { bubbles: true }));
el.dispatchEvent(new Event("change", { bubbles: true }));
el.dispatchEvent(
new KeyboardEvent("keyup", { bubbles: true, cancelable: true, key })
);
}
return true;
};
But, even this one also doesn’t work.
Is there any solution or workaround so as I can type the text to that input field programmatically?
2
Answers
Special thanks to Meijing Xue for sharing with me the that Google Finance is having multiple input tags, and with querySelectorAll, element value can be set.
After digging in, I discovered that in actual there are only two input elements with two duplicate elements, one each.
Solution:
Thanks again, Meijing bro :)
I’ve tried just now, there are 4 Input tag in https://www.google.com/finance/ page, when i use single querySelector and set value, nothing changed.
But when i change to querySelectorAll, it works
But it still makes me weird, single querySelector works in normal pages, maybe google changed getters and setters or something stuff