我正在尝试使用完整的XPath来获取WebView2和document.evaluate的DIV值,但是我找不到这样做的方法。
我读过很多东西,我认为我需要这样的东西:
Dim path As String = "/html/body/div[9]/div[1]/div/div/div/div/div[2]/div/div[5]/div/div/div/div/div[4]/div[2]/div/div[1]/div[2]/div"
WebView21.ExecuteScriptAsync("document.evaluate(path, document, Null, XPathResult.FIRST_ORDERED_NODE_TYPE, NULL)")但我不能轻易得到一些功能代码。
有什么帮助吗?
发布于 2022-02-03 15:38:34
@ Poul :谢谢,现在我可以运行这个程序了,但是DateStr字符串保持为null
这是我的新代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim URL As String
URL = "my url here"
WebView21.Source = New Uri(URL, False)
Timer1.Interval = 25000 '25s
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
GetPageInfoAsync()
End Sub
Private Async Function GetPageInfoAsync() As Task
Dim DateStr As String
Dim path As String = "/html/body/div[9]/div[1]/div/div/div/div/div[2]/div/div[5]/div/div/div/div/div[4]/div[2]/div/div[1]/div[2]/div"
DateStr = Await WebView21.ExecuteScriptAsync("document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)")
End Function知道有什么不对吗?
编辑:
现在这是一个答案。:-)
我在这个论坛上提出了一个非常简单的解决办法:
DateStr = Await WebView21.ExecuteScriptAsync("document.documentElement.outerHTML;")现在我可以在字符串中搜索我需要的数据,我将尝试使用xpath搜索数据。
编辑:
对不起,这个解决方案不起作用。我很沮丧。
使用webview允许等待25s来呈现网页。但是,在呈现的网页中,只有两行可见的DIV和滚动来查看其他行。不幸的是,代码:
DateStr = Await WebView21.ExecuteScriptAsync("document.documentElement.outerHTML;")只返回可见元素。带有滚动的DIV的不可见线不在字符串中。
如果有人有解决方案,我必须打开一个网页,等待25s才能呈现该页面,然后使用Xpath访问一些DIV数据,如: /html/body/div9/div1/div/div/div/div/div2/div/div5/div/div/div/div/div4/div2/div/div1/div2/div。
谢谢!
https://stackoverflow.com/questions/70970097
复制相似问题