Sir..
I want to get the values from web pages using vb.net..

THIS WORKS!

[B]PHP[/B]
<input type="text" name="sample" [B]id[/B]="sample" value="" />

[B]VB.NET[/B]
TextBox.Text = WebBrowser1.Document.[B]GetElementById[/B]("sample").GetAttribute("value")

but.. what if there's no id.

[B]PHP[/B]
<input type="text" [B]name[/B]="sample" value="" />

[B]VB.NET[/B]
TextBox.Text = WebBrowser1.Document.[B]GetElementById[/B]("sample").GetAttribute("value")  ??? 

it cant read the value since there's no id.

i search the web but there's no appropriate solution..

Recommended Answers

All 15 Replies

You can use - GetElementFromPoint and GetElementFromTagName.

You can use - GetElementFromPoint and GetElementFromTagName.

the code might change so..
ill go for GetElementFromTagName :D

any example sir? considering my example above.

Dim htmlElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")

        For Each el As HtmlElement In htmlElements
            If el.GetAttribute("name").Equals("sample") Then
                TextBox.Text = el.GetAttribute("value")
            End If
        Next

thanks Ox :D

how about this..

<img src="www.sample.gif" />

i need to get the value of src ( www.sample.gif in this case ) ..

:)

Well. Considering that a page might hold a number of this kind of tags it could prove to be a challenge.
You can use the same technique as I showed you and iterate through all the GetElementsByTagName("img") elements.
And you might have to provide some logic inside the loop to find the specific image you're looking for.

Dim htmlElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
        Dim imgList As List(Of String)
        For Each el As HtmlElement In htmlElements
            ' Provide additional logic if necessary
            If Not el.GetAttribute("src").Contains("sample") Then
                TextBox.Text = el.GetAttribute("src")
            End If
        Next

nice! :D

the IF NOT function will do..
how about clicking a specified coordinates in a webbrowser? :D

' Private class variable to get Document from WebBrowser control.
    Private WithEvents _document As HtmlDocument

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        _document = WebBrowser1.Document
    End Sub

    Private Sub _document_Click(ByVal sender As Object, ByVal e As HtmlElementEventArgs) Handles _document.Click
        '' Here's the coordinates
        'e.ClientMousePosition
        Dim htmlElement As HtmlElement = _document.GetElementFromPoint(e.ClientMousePosition)
        If htmlElement.GetAttribute("").Contains("") Then
            TextBox.Text = htmlElement.GetAttribute("")
        End If
    End Sub
' Private class variable to get Document from WebBrowser control.
    Private WithEvents _document As HtmlDocument

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        _document = WebBrowser1.Document
    End Sub

    Private Sub _document_Click(ByVal sender As Object, ByVal e As HtmlElementEventArgs) Handles _document.Click
        '' Here's the coordinates
        'e.ClientMousePosition
        Dim htmlElement As HtmlElement = _document.GetElementFromPoint(e.ClientMousePosition)
        If htmlElement.GetAttribute("").Contains("") Then
            TextBox.Text = htmlElement.GetAttribute("")
        End If
    End Sub

oww.. i dont get it..

where should i put the coordinates?
example.. click the webbrowser.. 100 from left.. 120 from top.. :D

Oh, I misunderstood.
The code i provided can only give you the coordinates of where You clicked.
I don't think it's possible to send a click to the browser by coordinates.
Unless someone else has a brilliant idea of how to do that.

to get the coordinates..
TextBox.Text1 = e.ClientMousePosition.X 'distance from left
TextBox.Text2 = e.ClientMousePosition.Y 'distance from the top

but setting the coordinates. gives me an error.. :(
e.ClientMousePosition.Y = TextBox.Text2

e.ClientMousePosition is the position of the mouse pointer depending on the client area, ie the webbrowser control.
So I'm thinking that it's not possible. Or at the very least very very difficult. :|

I'm also thinking that you might have to use the code snippets I've provided to find all clickable elements in the document.
Iterate throgh them until you find one you're looking for and extract the link.
You can then use that link to create an URI object and navigate to it. webbrowser1.Navigate(New Uri("http://a_link"))

e.ClientMousePosition is the position of the mouse pointer depending on the client area, ie the webbrowser control.
So I'm thinking that it's not possible. Or at the very least very very difficult. :|

I'm also thinking that you might have to use the code snippets I've provided to find all clickable elements in the document.
Iterate throgh them until you find one you're looking for and extract the link.
You can then use that link to create an URI object and navigate to it. webbrowser1.Navigate(New Uri("http://a_link"))

ahh.. i quit.. lol

how about this :D

PHP:
<span id="sample">welcome</span>

how to get the word "welcome"? :)

By using the property InnerHTML.

By using the property InnerHTML.

can you give me samples using this?

PHP:
<span id="sample">welcome</span>

this is the last one.. :D

txtBox1.Text = webbrowser1.Document.GetElementById("sample").InnerHtml
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.