Check if an item exists in WebBrowser using BackgroundWorker

I use a background worker to manipulate some elements of my WebBrowser using vb.net.

For example, a background working RFIDReader will check if I have a specific link.

 ElseIf TerminalBrowser.Url.ToString.Contains(baseUrl + "box-office/ticket-verification") And rf_code <> "" Then ' Insert RF Code in "rf_code" hidden text field Me.Invoke(Sub() Me.TerminalBrowser.Document.GetElementById("rf_code").SetAttribute("value", rf_code.ToLower)) End If 

What happens if I touch my RFID card. It will include the corresponding value for my rf_code element in my browser.

Now, what I want to do, I want to check if the container itself exists ( synchronize-rfid ) (since it pops up). See Image for reference.

enter image description here

Here is our code for this.

 If Me.TerminalBrowser.Document.GetElementById("synchronize-rfid") IsNot Nothing Then ' Code here end if 

Link: stack overflow

The problem is that BackgroundWorker does not actually interact with the user interface according to the code above.

Is there any method to determine if this element exists using a background worker?

I think I did it, but it does not work.

 Dim synchronize_rfid = Me.Invoke(Sub() Me.TerminalBrowser.Document.GetElementById("synchronize-rfid")) If synchronize_rfid IsNot Nothing Then ' Code here end if 
+5
source share
1 answer

Try declaring the variable first, then set it during the call (which will set it from the main thread).

Sub() lambda behaves like a regular Sub() method - End Sub , which means you can do the same as you have a separate method.

This works for me:

 Dim synchronize_rfid As HtmlElement If Me.InvokeRequired = True Then Me.Invoke(Sub() synchronize_rfid = Me.TerminalBrowser.Document.GetElementById("synchronize-rfid")) Else synchronize_rfid = Me.TerminalBrowser.Document.GetElementById("synchronize-rfid") End If If synchronize_rfid IsNot Nothing Then ' Code here End If 
+2
source

Source: https://habr.com/ru/post/1242832/


All Articles