Invoking a click on a class which is only used once - html

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.

Related

Visual Studio - getElementsByTagName() -

I am brand new with Visual Basic and am looking to create a program that loads up a browser and logs you in to a specific site.
However, the username and password fields of the web site that I am attempting to log into have no id or name to pull by.
Is there a way to GetElementByClass?
Any help is appreciated. I cannot seem to find much documentation online.
Similar to:
WebBrowser1.Document.GetElementById("username").SetAttribute("Value",yourUsername)
For example, the input box I am trying to get is:
<input type="text" class="gwt-TextBox" maxlength="50" style="width: 180px;">
So far, this is what I have come up with:
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("className").ToString
'MessageBox.Show(curElement.GetAttribute("className").ToString)
' This shows classNames of gwt-TextBox and gwt-PasswordTextBox
Dim user As String = "user"
Dim pass As String = "pass"
WebBrowser1.Document.All.GetElementsByName("gwtTextBox").SetAttribute("value", user)
WebBrowser1.Document.All.GetElementsByName("gwt-PasswordTextBox").SetAttribute("value", user)
I am now seeing that SetAttribute is not a member of HtmlElement Collection.
Full Code, with help of #Jimi (https://stackoverflow.com/users/7444103/jimi)
Dim inputElementCollection As HtmlElementCollection
inputElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
Dim buttonCollection As HtmlElementCollection
buttonCollection = WebBrowser1.Document.GetElementsByTagName("button")
For Each curElement As HtmlElement In inputElementCollection
Dim inputName As String = curElement.GetAttribute("className").ToString
'MessageBox.Show(curElement.GetAttribute("className").ToString)
'gwt-TextBox
'gwt-PasswordTextBox
Dim username = uname.Text
Dim password = passwd.Text
If curElement.GetAttribute("className") = "gwt-TextBox" Then
curElement.SetAttribute("value", username)
End If
If curElement.GetAttribute("className") = "gwt-PasswordTextBox" Then
curElement.SetAttribute("value", password)
End If
Next
For Each button As HtmlElement In buttonCollection
Dim buttonName As String = button.GetAttribute("className").ToString
If button.GetAttribute("className") = "gwt-Button" Then
button.InvokeMember("click")
End If
Next

VBA Access object code for DropDownList ContentControls

I am coding an Access database that will collect user input, then open a Word document and fill out various parts of the document.
The problem I am having is it will only work once for Drop Down Lists. Not sure why or where to look to fix this one. There are three types of items I am programmatically filling in. The first is bookmarks, no problem with this. Second is Content Control Checkboxes, these also work with no problems. The third is Content Control Drop Down Lists, this is where the problem is. First time I open the Access Database it works, but if I click the command button again, nothing (for Drop Downs). The main problem is that it doesn't produce an error message, so I am not sure where to look.
I am guessing it has something to do with the objects I am creating to do the drop down updates? any help would be great:
Dim WordApp As Word.Application
Dim strTemplateLocation As String
Dim dir As String
Dim path As String
Dim wDoc As Word.Document
path = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\"))
strTemplateLocation = path & "UserDoc.docx"
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, newtemplate:=False
With WordApp
'Working Bookmark
.Selection.GoTo what:=wdGoToBookmark, Name:="COMPANY": .Selection.TypeText [fac]
'Working checkbox
If Me.RD = True Then: .ActiveDocument.ContentControls(9).Checked = True
'Works ONCE drop down
Dim objCC As ContentControl
Dim objCE As ContentControlListEntry
Dim ListSelection As String
ListSelection = Me.System_Type.ListIndex + 2
Set objCC = ActiveDocument.ContentControls(1): Set objCE = objCC.DropdownListEntries.Item(ListSelection): objCE.Select
End With
Should I be closing out the objCE and objCC at the end or something?
This is probably your problem:
Set objCC = ActiveDocument.ContentControls(1)
It should be
Set objCC = .ActiveDocument.ContentControls(1)
But much better would be:
Set wDoc = WordApp.Documents.Add(Template:=strTemplateLocation, newtemplate:=False)
and then always use wDoc instead of WordApp.ActiveDocument.
See here: VBA ActiveDocument Concerns / Alternatives?

VB.NET/GetElementByClass how to div class click?

</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

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.