I have a chromiumBrowser(cefsharp)
in my solution and I want to perform a regular Google Map
search using the search bar in Google Maps.
I have basically no Javascript/Lambda
experience, but I have done my best with googling and have only come up with reading what is in the searching box, filling it out, then click()ing
the search button next to it.
The issue is, it only sometimes takes the first click()
. Google Maps seems to need at least a real keyboard input change event on the search Bar.
I need a search rather than a load(URL)
because search smoothly scrolls to the new destination, vs load(URL) which will white screen and then warp to the destination.
I've tried using simulated coordination mouse clicks (I can open the menu panel, yay?) but not trigger the searchbox triggers
. I've tried click() on the searchbox, but I can't get keydown
or keypress to work with my limited JS/HTML knowledge.
private void button1_Click(object sender, EventArgs e)
{
cBrowser.ExecuteScriptAsync("document.getElementById('searchboxinput').value='" + textBox1.Text + "'");
}
private void button2_Click(object sender, EventArgs e)
{
cBrowser.ExecuteScriptAsync("document.getElementById('searchboxinput').focus({})");
cBrowser.ExecuteScriptAsync("document.getElementById('searchboxinput').places_changed()");
cBrowser.ExecuteScriptAsync("document.getElementById('searchboxinput').focus({})");
}
private void button3_Click(object sender, EventArgs e)
{
cBrowser.ExecuteScriptAsync("document.getElementById('searchbox-searchbutton').click()");
}
private void button4_Click(object sender, EventArgs e)
{
int x = 25; int y = 60;
cBrowser.GetBrowser().GetHost().SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.None);
Thread.Sleep(15);
cBrowser.GetBrowser().GetHost().SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None);
}
So it takes the first click()
. But the second and thereon, the button click() does nothing. It acts as though the new input in searchbox never happened and the searchBox reverts.
The button click() only takes if I really click on the searchBox and let the triggers fly. Can someone please help me figure out the code to trigger the underlying requirements of the searchbox so I can click() the searchbox to search to the next area?
Update Using How do I view events fired on an element in Chrome DevTools? I think I have the events, but now how do I send that same event to Google Maps and have it cascade and run all the other functions a regular click/type triggers?
textInput > TextEvent {isTrusted: true, data: "a", view: Window, detail: 0, sourceCapabilities: null, …}
input > InputEvent {isTrusted: true, oM: "input", data: "a", isComposing: false, inputType: "insertText", …}
I'm assuming I have to send a textInput/input event with a textEvent/inputEvent Object. I cannot create neither in C#, so I guess I have to lump it all into a single js script, the element, event, and event object somehow?
I think I have to do something like this. But the default InputEvent is super large, and I can't find the constructor neither.
cBrowser.ExecuteScriptAsync("document.getElementById('searchboxinput').dispatchEvent(new InputEvent(isTrusted=true, data=\"f\", isComposing=false, inputType=\"insertText\", dataTransfer=null))");
"Uncaught TypeError: Failed to construct 'InputEvent': parameter 2 ('eventInitDict') is not an object.", source: about:blank (1)
And am I wrong to think that data="f" is not correct and I have to feed it a eventInitDict? Where would I even get or find that!
Does anyone have an idea or an example code that can trigger this? Even just a JS code might help (might be able to just plug that into ExecuteScriptAsync?).