Get full class name with VBA / Selenium - vba

I am using VBA and selenium to automate Chrome.
The html code looks like this:
<div class="field Name">...</div>
<div class="field Note">...</div>
<div class="field label">...</div>
webBrowser.FindElementByClass("field Name") doesn't work.
If have to use a loop like this:
lngCount = webBrowser.FindElementsByClass("field")
For i = 1 To lngCount
If webBrowser.FindElemetsByClass("field").Item(lngCount).??? = "field Name" Then
End if
Next i
I don't know the part where the ??? is.
How can I compare the complete class name with the string "field Name"?

I found a solution:
With XPath you can check the full string including spaces:
webBrowser.FindElementByXPath("div[#class='field Name']")
And so you can do something if it exists:
If Not webBrowser.FindElementByXPath("div[#class='field Name']") Is Nothing Then
'element exists, do something
Else
'element doesn't exists
End if

.Attribute("class") should work:
lngCount = webBrowser.FindElementsByClass("field")
For i = 1 To lngCount
If webBrowser.FindElemetsByClass("field").Item(lngCount).Attribute("class") = "field Name" Then
End if
Next i

"webBrowser.FindElementByClass("field Name") doesn't work"
It's because there can be multiple elements with the same class name. Hence it should be:
webBrowser.FindElementsByClass("field Name")
Note the s for plural elements: webBrowser.FindElement s ByClass("field Name")

Related

How to Target Specific Element in Internet Explorer using Access VBA

First off, please forgive me for any mistakes in posting - this is my first time posting (although I'm a frequent visitor!
I'm working on a program to improve productivity within my workgroup. I am attempting to link an Access database to an internal web application (Pega based) that is very complex. Unfortunately, my knowledge of coding is very basic, and I do not have the available funding or bandwidth to obtain formal assistance. While I would love to use add-ins, I cannot, since my company will not allow it (I've asked, begged, pleaded, etc).
This is what the page of the program looks like. I first need to click the Work Manager Tab (WMT) blocked in GREEN, then I need to click the Get Next button (NGN) blocked in RED.
Steps I think need to happen
Bind the correct IE window session
Click on WMT
Click on NGN
I have been able use Access to bind the correct window and tab of IE (thank you to the person who provided the basic code for this!) but have not yet been able to decipher how to target the correct element on the page. The naming structure is a bit too much for my knowledge.
This is a screenshot of the HTML for the WMT (it won't copy it, at least not legibly, even after cleanup). I would love to be able to use the ID for the specific element, however, it's dynamic, and changes with each interaction, which is why I've been trying to target the static pieces, highlighted in green.
I haven't done much work with the second piece, however, This is the screenshot for that, with the button highlighted in yellow, if anyone wants to help there, too.
Thank you!
CODE FOR THE WINDOW BIND (works all the time so far!)
Sub C360WindowFind()
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title Like "Coverage User" & "*" Then
Set C360Window = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next
If marker = 0 Then
MsgBox ("C360 window is not found. Please ensure C360 is open in Internet Explorer and try again")
Else
'DO THE OTHER THINGS
End If
End Sub
CODE I'VE TRIED TO TARGET CORRECT TAB (Add in place of "DO THE OTHER THINGS" in previous code set)
FIRST TRY (does some crazy stuff that ends up making the program log out)
FindWorkManager1()
Dim C360doc As HTMLDocument
Dim Element
Dim ULTabName As String
Dim LIElement As String
Dim WMT_HREF As String
ULTabName = "yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList"
ULElement = "getElementsByClassName"
LIElement = "getElementbyID"
WMT_HREF = "getElementsByTagName"
Set C360doc = C360Window.document
With C360doc.all
If element = ULElement("ULTabName").LIElement("Work Manager").innerText.WMT_HREF("a")(0) Then
element.Click
Else
MsgBox ("Please ensure Work Manager is open, then try again")
End If
End With
End Sub
SECOND TRY: Gets to the lines with stars and quits (tried a couple variations of this with same results)
FindWorkManager2()
Dim C360doc As HTMLDocument
Set C360doc = C360Window.document
TabTagCounter = 0
TabItemCounter = 0
With C360doc.all
Set Header = C360doc.getElementsByClassName("yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList")
'Find the correct TabTag within Header
For Each TabTag In Header
If TabTag.tagName Like "UL" Then
TabTagCounter = 1
'If we've identified the correct TabTag, then look at the TabItem
'*************Not currently working beyond this point :(
If TabTagCounter = 1 Then
For Each TabItem In TabTag
If TabItem.innerText Like "*Work Manager*" Then
TabItemCounter = 1
End If
MsgBox ("try again")
Next
Else
End If
End If
Next
End With
End Sub
I think you should try to click the <a> tag as it's a link navigating to #Tab2. If the class yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList is the only one, you could locate ul with this class name first, then locate the li and a with tag name.
As the real link is not accessible, so I make a test page like below:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="tStrCntr">
<ul class="yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList">
<li tabindex="-1"title="Work Manager"></li>
<li tabindex="0" title="Home" id="Tab2">
<a tabindex="-1" id="TABANCHOR" href="https://www.google.com">
<span style="display: inline-block">
<label>Work Manager</label>
</span>
</a>
</li>
</ul>
</div>
</body>
</html>
And the VB code to click Work Manager in the test page is like below, you could have a test:
Updated version:
Sub LOADIE()
Dim link As Object
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "http://the test web page"
Do Until ieA.readyState = 4
DoEvents
Loop
Set doc = ieA.Document
Set ElementCol = doc.getElementsByClassName("yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList")(0).getElementsByTagName("li")
For Each link In ElementCol
If link.getAttribute("title") = "Work Manager" Then
link.getElementsByTagName("a").Item(0).Click
End If
Next link
End Sub

Changing HTML elements with Webbrowser

I have a question, how to change HTML input value?
<select name="sys_lenght" aria-controls="systable" class>
<option value="20">Value 1</option>
<option value="40">Value 2</option>
<option value="80">Value 3</option>
</select>
I used this code
For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
If Elementz.Name = "sys_lenght" Then
Elementz.setAttribute("value", "80")
End If
Next
But it doesn't change the input value, only the text "Value 3".
How can I solve this problem? Thanks
sys_lenght's value indicates which option with the specified value is selected. Thus if you set sys_lenght.value to "80" it will select Value 3.
To change the value of the currently selected option you have to get a reference to that first. You can do so by getting the selectedIndex of sys_lenght, and then get the specific item from that index.
For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
If Elementz.Name = "sys_lenght" Then
'Get the index of the selected option.
Dim SelectedIndex As Integer = Integer.Parse(Elementz.GetAttribute("selectedIndex"))
If SelectedIndex >= 0 Then
'Get the option element from the resulting index.
Dim SelectedOption As HtmlElement = Elementz.Children(SelectedIndex)
SelectedOption.setAttribute("value", "80")
Else 'No option selected.
MessageBox.Show("No option selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Exit For 'We found what we were looking for; stop looping.
End If
Next
First you have to understand how html open and close tag, you forgot to close the html selection option open tag i mean the last greter sign > then use javascript query to find the value within the option example
WebControl1.ExecuteJavascript('document.querySelector('option [value='your value']').selected=true;")
hope its help?

GetElementById isn't working on certain boxes

I'm trying to make an automatic login in my program.
This is the code I'm using to find the email box and then set the value:
WebBrowser1.Document.GetElementById("email").SetAttribute("value,", ID & "#hotmail.com")
If I insect the element of the email box it says input class="textbox" type="email" name="email"
What am I doing wrong? :L
The element doesn't necessarily have to have an ID.
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname(v=vs.110).aspx
If that's the case, you should take a look at getting a collection via GetElementsByTagName and looping through - something along these lines:
Dim Elems As HtmlElementCollection
Elems = WebBrowser1.Document.GetElementsByTagName("input")
For Each elem As HtmlElement In Elems
Dim nameValue As String = elem.GetAttribute("name")
If nameValue.ToLower().Equals("email") Then
elem.SetAttribute("value,", ID & "#hotmail.com")
End If
Next
Refer to id instead of name. i.e. "email_ema" instead of "email"
input name="email" type="button" id="email_ema" value="+" style="display:None;"...
WebBrowser1.Document.GetElementById("email_ema").SetAttribute("value,", ID & "#hotmail.com")

Block a certain element in web browser Vb.net

I a wondering if and how you would be able to block a certain element in the webbrowser.
For example there is a certain element on a website:
<div class="btn" id="button" value="ClickMe"/>
Will you be able to block or hide that specific element in the webbrowser?
There might be a certain ad, iframe or whatever its called that might just be in the way?
Thanks for the help I appreciate it.
double click on webbrowser1 designer you should be in WebBrowser1_DocumentCompleted add this line
'selector by division id
webbrowser1.Document.getElementById("button").innerHtml = ""
'selector spesific element with spesific attribute
<input id="_authentication_token" name="_authentication_token" type="hidden" value="11740686"/>
dim mycol as htmlElementCollection = webbrowser1.document.getElementBytagname("input")
for each ele as Htmlelement in mycol
if (ele.getAttribute("value").tostring = "11740686") then ele.innerHtml = ""
next
for your exemple :
<div class="btn" id="button" value="ClickMe"/>
dim mycol as htmlElementCollection = webbrowser1.document.getElementBytagname("div")
for each ele as Htmlelement in mycol
if (ele.getAttribute("value").tostring = "ClickMe" and ele.getAttribute("id").tostring = "button" ) then ele.innerHtml = ""
next

How to select drop down value - Vb.net Webbrowser

I want to select a dropdown value with vb.net webbrowser
<OPTION value=1>One</OPTION>
<OPTION value=2>Two</OPTION>
The values 1 & 2 are without the quotes
If the value is within quotes such as value = "1"
Then I can use the code
WebBrowser1.Document.GetElementById("ID").SetAttribute("Value", "1")
But it does not work for the above.
Thanks in advance.
Actually it does not matter if there is a quote or not. I created test code and it seems to be working.
For Each Frame As HtmlWindow In currentWindow.Frames
Dim btnElementCollection As HtmlElementCollection =
Frame.Document.GetElementsByTagName("Select")
For Each curElement As HtmlElement In btnElementCollection
Dim controlName As String = curElement.GetAttribute("id").ToString
If controlName = TextBox2.Text Then
curElement.SetAttribute("Value", TextBox3.Text)
End If
Next
Next
TextBox2 is the id TextBox3 is the value
Sorry, not sure why it didn't work the first time and thanks for everyones time.
Try setting the attribute name to uppercase.
<OPTION VALUE=1>One</OPTION>
<OPTION VALUE=2>Two</OPTION>
Note "value" is now "VALUE"
You should set id of the target element.
This should be
GetElementById("ID")
this
GetElementById("element id ")
View your page source to get the correct id.