Can't login to website (VB.net browser) - vb.net

I try to login here: http://www.swagbucks.com/p/login
I've tried the following methods:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
If element.Id = "sbxJxRegEmail" Then
Dim i = 0
Dim strLine = TextBox2.Text
element.OuterText = strLine
End If
If element.Id = "sbxJxRegPswd" Then
Dim i = 0
Dim strLine = TextBox3.Text
element.OuterText = strLine
End If
Next
End Sub
and then I tried this
WebBrowser1.Document.GetElementById("sbxJxRegEmail").SetAttribute("value", TextBox2.Text)
WebBrowser1.Document.GetElementById("sbxJxRegPswd").SetAttribute("value", TextBox3.Text)
WebBrowser1.Document.GetElementById("loginBtn").Focus()
WebBrowser1.Document.GetElementById("loginBtn").InvokeMember("click")
What happens is that the text is entered, but on the browser, it's not really there. It's hard to explain, but basically the username and password are just floating on top of the text boxes like some sort of ghost text and when I then click it, it disappears.
How do I fix this?

It's possible that they are masked inputs, probably need to set the inner text of those fields like so...
WebBrowser1.Document.All("Username").SetAttribute("value", "myusername")
WebBrowser1.Document.All("Password").SetAttribute("value", "mypassword")
Dim elements As HtmlElementCollection = (WebBrowser1.Document.All.GetElementsByName("Password"))
For Each element As HtmlElement In elements
element.InnerText = "mypassword"
Next
'You may have to do the same for your username/email

Related

VB .Net navigate each link and get a specific url

I'm having trouble using the web browser in VB .NET to navigate a list of URL's inside a multiline textbox, wait for them for load and get a specific URL (should contain a specific word).
What I have until now:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim webClient As New System.Net.WebClient
'Dim htmlDoc As New HtmlAgilityPack.HtmlDocument()
For Each strLine As String In TextBox4.Text.Split(vbCrLf)
Console.WriteLine("Turning False")
Console.WriteLine("----")
WebBrowser1.Navigate(strLine)
Console.WriteLine("Waiting for navigation - " & strLine)
Console.WriteLine("Navigated")
Next
For Each link In links
TextBox6.Text = TextBox6.Text & link & vbCrLf
Next
Dim lines As New List(Of String)(TextBox6.Lines)
For i As Integer = lines.Count - 1 To 1 Step -1
If lines(i) = lines(i - 1) Then
lines.RemoveAt(i)
End If
Next
TextBox6.Lines = lines.ToArray
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
For Each ele As HtmlElement In WebBrowser1.Document.Links
'Get whatever text there is in the 'href' attribute
Dim eletarget As String = ele.GetAttribute("href")
'Add it to the listbox
If eletarget.Contains("cfsecure") Then
links.Add(eletarget)
'Carry on to the next link
End If
Next
End Sub
First of all, it seems it doesn't wait until fully loaded. After that, I don't get any link from what I asked (don't know if it's related to the first problem I have or any mistake in my code getting the href with the word "cfsecure").
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
For Each ele As HtmlElement In WebBrowser1.Document.Links
'Get whatever text there is in the 'href' attribute
Dim eletarget As String = ele.GetAttribute("href")
'Add it to the listbox
If eletarget.Contains("cfsecure") Then
links.Add(eletarget)
'Carry on to the next link
End If
Next
End Sub
What I'm missing here?

How to build a for each with a customized Module

Hi i have build a customized module do get elements by classname but i can not get work with for each function.
Well i try to get the loop on the code that call my elements by classname wich is a customized module
Normally its easy to get the loop but with this Customized Module , i can not find the way to this to work.
I am new yet on vb , and i can not find any example to do it wright
This is my code i need to perform a loop
I need some some thing like this
For Each cuele In ele
Next
But its give me an error
Expression is of type 'System.Windows.Forms.HtmlElement', which is not a collection type
I have try in a different way but i get a null exception
Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form3
Dim WithEvents htmldoc As HtmlDocument
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'TextBox1.Text = ""
WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
htmldoc = WebBrowser1.Document
'WebBrowser1.Document.Forms(0).InvokeMember("click")
'WebBrowser1.Document.Forms(0).InvokeMember("submit")
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
'ListBox1.Items.Add(webpageelement.GetAttribute("title").ToString)
'ListBox1.Items.Add(webpageelement.GetAttribute("href").ToString)
Dim PageElement As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("time")
For Each CurElement As HtmlElement In PageElement
'TextBox1.Text = TextBox1.Text + CurElement.GetAttribute("Liga NOS") + Environment.NewLine
'ListBox2.Items.Add(CurElement.GetAttribute("datetime") + Environment.NewLine)
Next
Dim PageElement2 As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("td")
For Each CurElement As HtmlElement In PageElement2
'TextBox1.Text = TextBox1.Text + CurElement.GetAttribute("Liga NOS") + Environment.NewLine
'ListBox1.Items.Add(CurElement.GetAttribute("data-content") + Environment.NewLine)
Next
Dim PageElement3 As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
For Each CurElement As HtmlElement In PageElement3
'TextBox1.Text = TextBox1.Text + CurElement.GetAttribute("Liga NOS") + Environment.NewLine
'ListBox3.Items.Add(CurElement.GetAttribute("alt") + Environment.NewLine)
Next
Next
For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
If ListBox1.GetItemText(ListBox1.Items(i)) = String.Empty Then
ListBox1.Items.RemoveAt(i)
End If
Next i
For i As Integer = ListBox2.Items.Count - 1 To 0 Step -1
If ListBox2.GetItemText(ListBox2.Items(i)) = String.Empty Then
ListBox2.Items.RemoveAt(i)
End If
Next i
Dim ele As HtmlElement = WebBrowser1.Document.GetElementsByClassName("league-data")(0)
'TextBox1.Text = ele.InnerText
ListBox2.Items.Add(ele.InnerText)
Dim ArrayTag As New ArrayList
For Each item As HtmlElement In
Form1.WebBrowser1.Document.GetElementsByTagName("div")
ArrayTag.Add(item.InnerText)
Next
ListBox2.Items.Add(ArrayTag(64))
End Sub
End Class
OK This might well work, but I can't test it. I've altered the extension a little, so that it creates a list rather than a HtmlElementCollection (It seems that these are read-only)
<System.Runtime.CompilerServices.Extension()>
Public Function GetElementsByClassName(Source As HtmlElementCollection, ClassName As String) As List(Of HtmlElement)
Dim output As New List(Of HtmlElement)
For i As Integer = 0 To Source.Count - 1
Try
If Source(i).GetAttribute("className") = ClassName Then
output.Add(Source(i))
End If
Catch ex As Exception
End Try
Next
Return output
End Function
And edited the form code so that it also deals with a list instead of the HtmlElementCollection
For Each cuele As HtmlElement In ele
ListBox2.Items.Add(cuele.InnerText)
Next
Hopefully this should work.
I also suggest that you use a list instead of an ArrayList as Lists are faster and strongly typed ..
Dim ArrayTag As New List(Of String)
For Each item As HtmlElement In Form1.WebBrowser1.Document.GetElementsByTagName("div")
ArrayTag.Add(item.InnerText)
Next
ListBox2.Items.Add(ArrayTag(64))

Textbox replace and split not working vb.net

I've been struggling way too much with simple things, like the one i'm posting.
I'm developing a UI in vb.net that gathers some information from a machine. The information is collected to a TextBox:
Private Sub ReceivedText(ByVal [text] As String)
If Me.TextBox2.InvokeRequired Then
Dim x As New SetTextCallBlack(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.TextBox2.Text &= [text]
End If
End Sub
Then i gather that information either to a datagridview or to some labels to display simple information.
Sub dgv()
Dim sup2 = TextBox2.Text.Replace("#", "").Replace(">", " "c)
Dim sup() = sup2.Split(" "c, "#", vbCrLf, vbTab)
With DataGridView1
.Rows(0).Cells(0).Value = sup(1).ToString
.Rows(0).Cells(1).Value = sup(7).ToString
.Rows(0).Cells(3).Value = sup(4).ToString
End With
Button5.Enabled = True
Button6.Enabled = True
End Sub
This works just fine !!!
But when i try to populate the labels, with the code below, it just won't work!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread.Sleep(250)
Dim final = TextBox2.Text.Replace("#", "").Replace("SN", " "c)
Dim final2() = final.Split(" "c, "#", vbCrLf, vbTab)
Label1.Text = final2(0).ToString
Textbox2.Text= final2(0).ToString
End Sub
Can someone help me? The label gets no text.. and the textbox gets all of it.
Btw, the textbox is multiline and if i paste the text in microsoft word it comes with tabs and extra spaces.
Edit: printscreen from microsoft word below [ related to Multiline Textbox to Datagridview ]
Edit2: This is so strange ..
If i do this
Label1.Text = "Testing" & TextBox2.Text
it only shows "Testing" on the label..
If you set the label to AutoSize, it will automatically grow with whatever text you put in it. (This includes vertical growth.)
Before assign value to the label1,
Use the below code
Label1.MaximumSize = new Size(100, 0)
Label1.AutoSize = true
your code would be like
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread.Sleep(250)
Dim final = TextBox2.Text.Replace("#", "").Replace("SN", " "c)
Dim final2() = final.Split(" "c, "#", vbCrLf, vbTab)
Label1.MaximumSize = new Size(100, 0)
Label1.AutoSize = true
Label1.Text = final2(0).ToString
Textbox2.Text= final2(0).ToString
End Sub

VB.NET How to get text from an HTML table?

I'm trying to access certain tables and values from a certain html page inside of a webbrowser control. Here's what i'm trying to access:
https://gyazo.com/c4312f860397d0f86ccce425d1fb3d48
In the end, I'm trying to access the value="100" inside of the input name="Server[players]". Is there any way to do this? I'm not using any external addons for visual studio or anything. I've already gotten this working:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim divs = WebBrowser1.Document.GetElementsByTagName("div")
For Each div As HtmlElement In divs
If div.GetAttribute("id") = ("statusdetail-ajax") Then
Dim status As String = div.InnerText
Label1.Text = status
End If
Next
Which just shows me the online/offline status. Any help is greatly appreciated!
You want the GetAttribute() method, like so:
Private Sub DisplayMetaDescription()
If (WebBrowser1.Document IsNot Nothing) Then
Dim Elems As HtmlElementCollection
Dim WebOC as WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("META")
For Each elem As HtmlElement In Elems
Dim NameStr As String = elem.GetAttribute("name")
If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
If NameStr.ToLower().Equals("description") Then
Dim ContentStr As String = elem.GetAttribute("content")
MessageBox.Show("Document: " & WebOC.Url.ToString() & vbCrLf & "Description: " & ContentStr)
End If
End If
Next
End If
End Sub
Taken from:
https://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.getattribute(v=vs.110).aspx
EDIT:
Here's the same concept adapted to your code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim inputs = WebBrowser1.Document.GetElementsByTagName("input")
For Each input As HtmlElement In inputs
If input.GetAttribute("id") = ("Server_players") Then
Dim status As String = input.GetAttribute("value")
Label1.Text = status
End If
Next
End Sub
In other words, InnerText returns everything between elements, while GetAttribute() returns an attribute inside an element.
<element attribute="Value">Inner Text</element>

trying to output more than one filenam into textbox vb.net

Apologies if this is really simple but I'm fairly new to programming. I've created a program that uses an open dialog box and outputs the names of the file to a textbox.
Where I'm having issues is trying to get the textbox to display more than one line as all it seems to be doing is writing one line in the textbox.
The code I'm using is below, could someone please advise what I need to change so that I can get this to work.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strFileName As String
OpenFD.Multiselect = True
OpenFD.InitialDirectory = "\\server\filename\"
OpenFD.Title = "Open a Text File"
OpenFD.Filter = "Text Files(.txt)|*.txt"
Dim DidWork As Integer = OpenFD.ShowDialog()
strFileName = OpenFD.FileName
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileName
TextBox1.Text = strFileName += 1
End If
End Sub
I've managed to get everything else to work correctly but it's just this one thing.
Dim strFileName() As String
'...
Dim DidWork As Integer = OpenFD.ShowDialog()
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileNames
TextBox1.Multiline = True
TextBox1.Text = ""
For Each sFile as String in strFileName
TextBox1.Text &= sFile & System.Enviroment.NewLine()
Next
End If
Set TextBox.Multiline property to True