@ducaisoft
“Question 1: how to use Html object to download a file from a given url like “http://www.ycbat.com.cn/downs/APIs.txt”
, please show me a full demo based on VB.net.”
In order to download a page located at a specific URL with all related resources, you can use the following code snippet:
Using document As New HTMLDocument("https://example.com/")
document.Save("out.html")
End Using
You can download a specific file like this:
Using document As New HTMLDocument()
Using response = document.Context.Network.Send(New RequestMessage("http://www.ycbat.com.cn/downs/APIs.txt"))
Dim result = response.Content.ReadAsString()
End Using
End Using
“Question 2: how to open a given url and then search one or more html-elements such as div,a,hfre,input,button,span,file,label,frame,form …,and then get/set the value or text for the element, and perform click action fort the element.”
In order to open a document located at a specific URL, you can simply create an instance of the HTMLDocument class using the URL-input constructor, as shown in the following example:
Using document As New HTMLDocument("https://example.com/")
' Do some work here
End Using
To find a specific element or elements in a document, you can use the following methods:
GetElementById
GetElementsByTagName
GetElementsByClassName
The following example shows how to find the “div” element in the tree, get its content, and then change it:
Using document As New HTMLDocument("<div>123</div>", String.Empty)
Dim element = document.GetElementsByTagName("div").First()
Dim oldText = element.TextContent
element.TextContent = "new content"
Dim newText = element.TextContent
End Using
In order to “click” on an element, you will need to create and send the corresponding event. The following example shows a document in which a “click” occurs on the “body” element, after which the corresponding script is triggered to change its content:
Using document As New HTMLDocument("
<script>
function click() {
document.body.innerHTML = 'new content';
}
</script>
<body onclick='click()'>
123
</body>", String.Empty)
Dim mouseEvent = document.CreateEvent("MOUSEEVENT")
mouseEvent.InitEvent("click", False, True)
Dim oldText = document.Body.TextContent
document.Body.DispatchEvent(mouseEvent)
Dim newText = document.Body.TextContent
End Using