Home > Mobile >  How to parse text with WebView2 control
How to parse text with WebView2 control

Time:01-01

I need to get the text that constantly changing from a website that does not load with the old Web Browser control, so I'm moving for the first time steps on WebView2

The html I want to parse is:

<td data-v-39c7db2a="" >
0.0000%
</td>

the value I need is the percentage. The code I'm using is

Private Sub WebView2_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebView2.NavigationCompleted

Dim text As String = Await WebView2.ExecuteScriptAsync("document.getElementById('text-right').selectedIndex")
MessageBox.Show(text)

It doesn just return a messagebox with "null" It's kind of complicated now to move from the old webbrowser control to the WebView, also because of the lack of documentation.

CodePudding user response:

This is not really associated with WebView2 but a pure javascript problem.

You need to use:

document.querySelector('td.text-right').textContent

This returns the text for the first table cell with class name 'text-right'.

However this might not work for you if there's more than one table cell with the class name `text-right'.

In that case you can query for the nth child, where n is the child index:

document.querySelector('td.text-right:nth-child(7)').textContent

To get the percentage value as a Double:

Dim percent as Double = Double.Parse(Regex.Match(text, "[\d.] ").ToString(), CultureInfo.InvariantCulture);
  • Related