GetElementById Two same ID's - vb.net

Hey I need help well im trying to make a program that uses a site to do some stuff and it has two of the same id's for both textboxs im trying to input into my code is WebBrowser2.Document.GetElementById("form-control-3").InnerText = BunifuMaterialTextbox1.Text. Well anyway im tryint to do that to input into the first textbox and the 2nd textbox has the same id and classname so i don't really know what to do.

You could try grabbing all the HTML elements based on the tag and then loop through to see if the id matched. Something like...
Dim Elems As HtmlElementCollection
Elems = WebBrowser2.Document.GetElementsByTagName("[tagName]")
For Each elem as HtmlElement in Elems
Dim idStr As String = elem.GetAttribute("id")
If ((idStr IsNot Nothing) And (idStr = "form-control-3"))
elem.InnerText = BunifuMaterialTextbox1.Text
End If
Next

i don't know if this help but you can do a input array
example:
<input name="name[]">
and...
document.getElementsByName("name[]")

Related

Is there a way to retrieve some text from a webpage to a textbox in VB?

I'm trying to make it so I can have several text boxes in my form show pieces of information from a specific webpage. For example, would there be a way I would be able to retrieve the title of this question to a variable with the click of a button in Visual Basic?
It not hard but you have to look at the source page, and identify the elements.
In nicely formed pages, usually the div elements have a tag ID, but often they don't, so you have to grab say by a attribute name - often you can use the class name of the div in question.
So, to grab the Title, and you question text of this post?
This works:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xDoc As New Xml.XmlDocument
Dim strURL As String = "https://stackoverflow.com/questions/55753982"
Dim xWeb As New WebBrowser
xWeb.ScriptErrorsSuppressed = True
xWeb.Navigate(strURL)
Do Until xWeb.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim HDoc As HtmlDocument = xWeb.Document
Debug.Print(HDoc.GetElementById("question-header").FirstChild.InnerText)
Debug.Print(FindClass(HDoc, "post-text"))
End Sub
Function FindClass(Hdoc As HtmlDocument, strClass As String) As String
' get all Divs, and search by class name
Dim OneElement As HtmlElement
For Each OneElement In Hdoc.GetElementsByTagName("div")
If OneElement.GetAttribute("classname") = strClass Then
Return OneElement.InnerText
End If
Next
' we get here, not found, so return a empty stirng
Return "not found"
End Function
OutPut:
(first part is title question)
Is there a way to retrieve some text from a webpage to a textbox in VB?
(second part is question text)
I'm trying to make it so I can have several text boxes in my form show pieces of
information from a specific webpage. For example, would there be a way I would be
able to retrieve the title of this question to a variable with the click of a button
in Visual Basic?

How can I play the first url that I get when I search in youtube with vb.net?

I trying to do a program in vb.net that will search a video on youtube by words and play the first url (the first result) that I will get without waiting to client to choose.
Someone can help me?
I did the search but I dont know how to do that it will play the first video that I will get automatically.
What form you choose to develop this program ? Webform ? Winform?
I assume that the form is winform, then you could use the "WebBrowser" (you can find the "WebBrowser" in the toolbox)
Private Sub openYouTube()
WebBrowser1.Navigate("https://www.youtube.com/?gl=TW")
End Sub
Find the search bar on the youtube (use GetElementByID) and insert the string which you want to search (Use SetAttribute). The ID of the inputbox of youtube search bar is "search" (How to find the ID of any element in a webpage? Target the element and click the right button of mouse, choose "inspect" for Chrome, "inspect element" for IE, it would show the element detail)
Private Sub inputString&SearchIt()
Dim theStringYouWantToSearch as string
WebBrowser1.Document.GetElementById("search").SetAttribute("value", theStringYouWantToSearch)
End Sub
Execute the search (Use InvokeMember("click") to fire the search)
Private Sub executeSearch()
WebBrowser1.Document.GetElementById("search-icon-legacy").InvokeMember("click")
End Sub
Find the first result and record the URL.
This process is more complicated, use GetElementsByTagName to get a collection of result tables, the first result is the first item of this collection. Find the table of first result then loop through all elements of this table, to find the element which id is "thumbnail", and this element has the information of part of URL.
Private Sub findFirstResult()
Dim elemCollection as HtmlElementCollection
Dim firstResultTable as HtmlElement
Dim hrefString as string
Dim resultURL as string
elemcollection = WebBrowser1.Document.GetElementsByTagName("ytd-video-renderer")
firstResultTable = elemCollection.item(0)
For each element as HtmlElement in firstResultTable.All
If element.Id = "thumbnail" then
hrefString = element.GetAttribute("href")
End if
Next
resultURL = "https://www.youtube.com" & hrefString
End Sub
Between every step, you have to wait for the complement of webpage loading, or the program wouldn't find the element. There are several ways to do that, you could do some search for that.

Get value of input in vb.net webbrowser?

I have the following input, I want to get it value using vb.net WebBrowser, and then put it into a variable how can i perform that?
<input type="hidden" name="loginurl" value="https://example.com?ctoken=KWYZlCrYxt">
I'm trying to use this code, but i don't know how to put the value i got into a variable:
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
'Depending on how the source code is formatted on the tag, you may also try Element.OuterHTML, Element.InnerText and Element.OuterText in the line below
If Element.OuterHtml.Contains("name=""loginurl""") Then
Element.GetAttribute("value")
Dim login = value
WebBrowser1.Navigate(login)
Exit For
End If
Drag and drop webbrowser control and use the following snippet
WebBrowser1.Navigate("https://example.com?ctoken=KWYZlCrYxt")
Here is a good reference
Hope this is useful
I'd say just assign a variable with Dim attributeValue = Element.GetAttribute("value")... am I missing something obvious here?
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
'Depending on how the source code is formatted on the tag, you may also try Element.OuterHTML, Element.InnerText and Element.OuterText in the line below
If Element.OuterHtml.Contains("name=""loginurl""") Then
Dim attributeValue = Element.GetAttribute("value")
Dim login = value
WebBrowser1.Navigate(login)
Exit For
End If
Try:
Dim AllElementes As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In AllElementes
If webpageelement.GetAttribute("name") = "loginurl" Then
Dim login = webpageelement.GetAttribute("value")
WebBrowser1.Navigate(login)
End If
Next
should work.

Pull webpage information to text box

So what I am trying to do is pull some data from a webpage, and put it into a text box. Online, I can find alot about doing it the other way around (putting text box into web) but none the way I want.
I have gotten this far... (not very) and have no clue where to go from here
Dim Helement As HtmlElement
Helement = WebBrowser1.Document.GetElementById("txtAddress1")
Helement.GetAttribute()
Here is the element from the browser I am trying to use
input#ctl00_m_g_79f90d63_a5eb_43e1_9403_69fefa0ba004_ctl00_txtAddress1
Thanks guys!
This is what I tried:
Dim Helement As HtmlElement = WebBrowser1.Document.GetElementById("txtAddress1")
Dim elementtype As String = Helement.GetAttribute("text")
Dim textContents As String = Helement.InnerText
Address.Text = textContents
The GetAttribute function will return the value of an attribute of the element you retrieved using GetElementById.
For example, something like:
<input type="text" id="txtAddress1">
Dim Helement as HtmlElement = WebBrowser1.Document.GetElementById("txtAddress1")
Dim elementType as String = Helement.GetAttribute("type")
Dim textContents as String = Helement.InnerText
InnerText MSDN
GetAttribute MSDN

Put text into a textbox from a website in VB.NET

I want to know how to put text to a website's textbox with VB.NET.
Like when you complete a textbox and press enter.
Something like this:
Dim web as webbrowser
web.document.textbox(1).insert("text")
it would look something like this:
WebBrowser1.Document.GetElementById("login_password").SetAttribute("value", TextBox2.Text)
As giuseppe has suggested, you need to search for the element in the document and then set it's value property. for example following code logins into website by setting its userId and password textboxes and clicking the submit button.
Dim htmlDoc=WebBrowser1.Document
Dim elem_Input_UsrName As HtmlElement = htmlDoc.GetElementById("username")
Dim elem_Input_PssWrd As HtmlElement = htmlDoc.GetElementById("password")
Dim elem_Input_Submit As HtmlElement = getElementByClassName("Submit", "Input", htmlDoc)
If elem_Input_UsrName IsNot Nothing AndAlso elem_Input_PssWrd IsNot Nothing Then
elem_Input_UsrName.SetAttribute("value", "xyz#ABC.com")
elem_Input_PssWrd.SetAttribute("value", "yourpassoword")
elem_Input_Submit.InvokeMember("click")
End If
This will help 100%:
webbrowser1.document.getElementById("yourtextboxidinwebsite").innertext = textbox1.text
I get a way that if u want to receive from the website itself another way I need to receive results from the website into the text box. U can use this method.
first, make a web browser to load the page
WebBrowser1.Navigate("https://tools.keycdn.com/geo")
then this
TextBox1.Text = WebBrowser1.Document.GetElementById("geoResult").InnerText