问题
I would like to simulate a click or keypresses to a web browser element that is on my visual studio vb project.
I have found ways to do it for the webbrowser object built-in to visual studio, but I am using the cefsharp browser, so
weBrowser.Document.GetElementById('id').InvokeMember("Click")
would not work, because cefsharp doesn't allow .Document
. So my question, to reiterate, is, how would I use vb to simulate a click on my cefsharp webbrowser? Any help is appreciated, and have a nice day.
EDIT: I've been working on this code:
Dim elementID As String = "myBtn"
Dim click As String = "Click"
browser.ExecuteScriptAsync("Document.All(elementID).InvokeMember(click)")
but I am not sure if it will work or how to use the elementID part (I am not sure what kind of web elements can go here). Maybe this extra information will help.
回答1:
Using ExecuteScriptAsync
will execute JavaScript against the Chrome engine, so you would have to send valid JavaScript to this function. The following code shows how you could start a search using DuckDuckGo
Imports System.Threading
Imports CefSharp
Imports CefSharp.WinForms
Public Class Form1
Private _browser As New ChromiumWebBrowser()
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_browser.Top = 10
_browser.Left = 10
_browser.Width = 600
_browser.Height = 400
Controls.Add(_browser)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_browser.Load("https://duckduckgo.com/")
'for simplicity just wait until page is downloaded, should be handled by LoadingStateChanged
Thread.Sleep(3000)
Dim jsScript As String = <js><![CDATA[
document.all("q").value = "stack overflow";
document.all("search_button_homepage").click();
]]></js>.Value
_browser.ExecuteScriptAsync(jsScript)
End Sub
End Class
来源:https://stackoverflow.com/questions/46874570/invoke-click-cefsharp-vb