VB.NET/GetElementByClass how to div class click? - html

</div></div></div><div class="f u" id="m_more_item"><span>Diğerlerini Gör</span></div></div></div></div></div></div></div></body></html>
DOCUMENT
CODE:
Dim h1 As HtmlElementCollection = Nothing
h1 = W.Document.GetElementsByTagName("div")
For Each curElement As HtmlElement In h1
If InStr(curElement.GetAttribute("classname").ToString, "f u") Then
curElement.InvokeMember("Click")
BUT code does not work HELP ME ?

Off my Example: VB.Net - select a class using GetElementByClass and then click the item programmatically
The problem is your trying to click the DIV instead of trying to click the A HREF tag. You will want to do that instead.
Since there is no "class" or anything on that element, then you could do something like...
Dim h1 As HtmlElementCollection = Nothing
h1 = W.Document.GetElementsByTagName("div")
For Each h1Element as HtmlElement in h1
Dim NameStr As String = h1Element.GetAttribute("href")
If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
h1Element.InvokeMember("Click")

Dim theElementCollection As HtmlElementCollection = Nothing
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element.GetAttribute("id") = "m_more_item" And Element.OuterHtml.Contains("Diğerlerini Gör") Then
Element.InvokeMember("click")
End If
Next

Related

Read multiple web classes, add them to listview

Hey I have following problem
I need to get specific values from website and more than one,
here's an example of website code
<div class="content">
<div class="all-items">
<div class="item1">
Example Item
</div>
<div class="itemsize">
" 103 "
<span> cm <span>
</div>
there more "item1" classes under the first with same name which I need to add in listview
until there is no "item1" class more.
I tried following but its not throwing an error or anything...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element.GetAttribute("className") = "content" Then
For Each Element0 As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element0.GetAttribute("className") = "all-items" Then
For Each Element1 As HtmlElement In Element.GetElementsByTagName("div")
If Element1.GetAttribute("className") = "item" Then
For Each Element2 As HtmlElement In Element.GetElementsByTagName("a")
If Element2.GetAttribute("className") = "href" Then
Dim vLink As String = Element2.InnerText
For Each Element3 As HtmlElement In Element.GetElementsByTagName("a")
If Element3.GetAttribute("className") = "Example Item" Then
Dim vTitle As String = Element3.InnerText
For Each Element4 As HtmlElement In Element.GetElementsByTagName("div")
If Element4.GetAttribute("className") = "itemsize" Then
Dim vSize As String = Element4.InnerText
For Each Element5 As HtmlElement In Element.GetElementsByTagName("span")
If Element5.GetAttribute("className") = "span" Then
Dim vUnit As String = Element5.InnerText
With lvList.Items.Add(vTitle)
.SubItems(0).Text = (vLink)
.SubItems(1).Text = (vSize + " " + vUnit)
End With
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
End Sub
its messy af but I should work theoretically except the size one
and I'm not sure to if I get all "item1" classes with that
I have literally no more idea at this point especially I failing
to retrieve just one value.
any suggestions or help?

get html element by id vb.net

I am trying to change the background color of a div when clicked. The div is inside a link button which is the sender in this function:
Public Sub ClickDiv(sender As Object, e As EventArgs)
Dim lnkbtn As LinkButton = TryCast(sender, LinkButton)
End sub
How can i get the div inside of this link button (the div has command argument as id). What I've tried so far (i am getting div=Nothing):
Dim div As HtmlControl = TryCast(lnkbtn.FindControl(lnkbtn.CommandArgument), HtmlControl)
or
Dim div As HtmlControl = TryCast(Page.FindControl(lnkbtn.CommandArgument), HtmlControl)
HTML in source code:
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$ctl21','')" style="text-decoration:none;">
<div id="x11" class="col-md-1" runat="server">11</div>
</a>
Creation of Link button and div:
Dim div As HtmlGenericControl = New HtmlGenericControl()
div.Attributes.Add("id", "y" & i)
div.Attributes.Add("class", "col-md-2")
div.Attributes.Add("runat", "server")
Dim lnkbtn As LinkButton = New LinkButton
AddHandler lnkbtn.Click, AddressOf Me.ClickDiv
lnkbtn.CommandArgument = "y" & i
lnkbtn.Controls.Add(div)
divrepeater.Controls.Add(lnkbtn)

Invoking a click on a class which is only used once

I'm trying to use a loop and I'm getting freezing but no full crash.
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("DIV")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
curElement.InvokeMember("click")
Next
In my case there should only be one div class name and I just want to invoke a click on it.
edit-improved formatting
I found a workaround like so
For Each h As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
If Not Object.ReferenceEquals(h.GetAttribute("className"), Nothing) AndAlso h.GetAttribute("className").Equals("numeroCustomer") Then
h.InnerText = loginid
Exit For
End If
Next
Hope this helps anybody with a similar problem.

get url from HTML string

I have the following code that grabs a div element:
For Each ele As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If ele.GetAttribute("className").Contains("description") Then
Dim content As String = ele.InnerHtml
If content.Contains("http://myserver.com/image/check.png") Then
'Do stuff if image exists
Else
'Do stuff if image doesn't exist
End If
End If
The div element looks like this:
<DIV class=headline><SPAN class=blue-title-lg>TITLE_HERE
</SPAN> LOCATION1_HERE, LOCATION2_HERE</DIV>DESCRIPTION_HERE<BR>
<DIV class=about><A class=link href="viewprofile.aspx?
profile_id=00000000">USERNAME</A> 20 FSM -
Friends <FONT color=green>Online Today</FONT></DIV>
When the tick image doesn't exist, I want to grab the url that's in:
<a class=link href="viewprofile.aspx?profile_id=00000000"></a>
and put it into a string. This is where I've hit a brick wall and I need some help. I'd imagine a regex solution would resolve my issue, but regex is one of my weak spots. Can someone put me out of my misery?
Solved it!
I slept on it and came up with a really simple way of solving it. The UI of my app now looks like a mess, but I'll sort that later. I have the information I need.
Here's how I did it:
Dim PageElement As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each CurElement As HtmlElement In PageElement
Dim linkunverified As String
linkunverified = CurElement.GetAttribute("href")
If linkunverified.Contains("viewprofile.aspx") Then
If ListBox1.Items.Contains(linkunverified) Then
Else
ListBox1.Items.Add(linkunverified)
End If
End If
Next
For Each ele As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If ele.GetAttribute("className").Contains("description") Then
Dim content As String = ele.InnerHtml
If content.Contains("http://pics.myserver.com/image/check.png") Then
Else
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
If content.Contains(ListBox1.Items(i).Remove(0, 24)) Then
ListBox2.Items.Add("http://www.myserver.com/" & ListBox1.Items(i).Remove(0, 24))
End If
Next
End If
End If
Next

ListBox with html element

Can anyone offer me some advice? I currently have a listbox I am using, in the listbox there is a list of images from any website. they are grabbed from the website via this method
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
For Each CurElement As HtmlElement In PageElements
imagestxt.Items.Add(imagestxt.Text & CurElement.GetAttribute("src") & Environment.NewLine)
Next
Timer1.Enabled = True
End Sub
I then use the picture control method to get the image and display it.
pic1.Image = New Bitmap(New MemoryStream(New WebClient().DownloadData(imagestxtimagestxt.SelectedItem.ToString))).SelectedItem.ToString)))
This method pulls the images and title from the HTML.
Private Function StrHTML12() As Boolean
Dim htmlDocument As HtmlDocument = WebBrowser1.Document
ListBox1.Items.Clear()
For Each element As HtmlElement In htmlDocument.All
ListBox1.Items.Add(element.TagName)
If element.TagName.ToUpper = "IMG" Then
imgtags.Items.Add(element.OuterHtml.ToString)
End If
If element.TagName.ToUpper = "TITLE" Then
titletags.Items.Add(element.OuterHtml.ToString)
Timer1.Enabled = False
End If
Next
End Function
This is a counting method to count how many empty alt="" or empty img alt='' there are on the page.
Basically what i am looking to do is;
Have a program that can check the image, look at the alt='' or img alt='' if on the website the dev hasn't put anything in the alt tag i want the image to show in a picture box and i want the alt tag either next to it or underneith it or something. but i have no idea how.
counter = InStr(counter + 1, strHTML, "<img alt=''")
counter = InStr(counter + 1, strHTML, "alt=''")
counter = InStr(counter + 1, strHTML, "alt=""")
The above seems really slow and messy. is there a better way of doing it?
I do not have VB installed so I have not been able to test the code. I'm also not familiar with the datagridview component so have not attempted to integrate my code with it.
The code below should get you the title of the page, and loop through all the img tags that do not have (or have empty) alt-text
HtmlElement.GetAttribute(sAttr) returns the value of the attribute or an empty string.
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim Title As String
Dim ImSrc As String
Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
// This line might need to be adjusted, see below
Title = PageElements.GetElementsByTagName("title")(0).InnerText
For Each CurElement As HtmlElement In PageElements
If CurElement.GetAttribute("alt") = "" Then
// CurElement does not have alt-text
ImSrc = CurElement.GetAttribute("src") // This Image has no Alt Text
Else
// CurElement has alt-text
End If
Next
Timer1.Enabled = True
End Sub
The line that gets the title might need to be changed as I'm unsure how collections can be accessed. You want the first (hopefully only) element returned from the GetElementsByTagName function.