mark all check boxes in internet explorer using excel vba - excel

I'm trying to create excel macro which will mark 75 check boxes in a certain table on internet explorer page
the code of that table is :
<TABLE id=ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes border=0><TBODY>
<TR>
<TD><INPUT id=ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_0 type=checkbox name=ctl00$MasterMain$ucGenConfig$ucConfigContainer$ucConfigPopup$cblSchemes$0><LABEL for=ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_0>Start</LABEL></TD></TR>
<TR>
<TD><INPUT id=ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_1 type=checkbox name=ctl00$MasterMain$ucGenConfig$ucConfigContainer$ucConfigPopup$cblSchemes$1><LABEL for=ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_1>Start2</LABEL></TD></TR>
<TR>
<TD><INPUT id=ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_2 type=checkbox name=ctl00$MasterMain$ucGenConfig$ucConfigContainer$ucConfigPopup$cblSchemes$2><LABEL for=ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_2>Default</LABEL></TD></TR>
<TR>
and so on
I have tried various ways but it doesn't want to play
With IE.document.getElementsByName("ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes")
.Item.Click
end with
and
With IE.document.getElementsByName("checkBoxlist(ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes)")
.Item(0).Checked = True 'Entered
End With
and
For Each htmlelement In IE.document.getElementsByName("ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes")
htmlelement.Item(0).Click
Next htmlelement
Thanks in advance for any help or leads :)

I am making an assumption here that the checkboxes that have an id like this:
ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_0
Have an identifier that begins at 0, as above, and increments by 1 to 74 (corresponding to your request for 75 checkboxes).
If that is the case, something like this may work:
Dim sBaseName As String
Dim i As Integer
'The base id of the checkboxes
sBaseName = "ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_"
For i = 0 To 74 '75 checkboxes
ie.Document.getElementByID(sBaseName + CStr(i)).Click
Next i
Of course you must be sure you have set ie appropriately, navigated to the page, etc.
This code will first grab and click
ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_0
Then i increments by 1, so it will grab and click
ctl00_MasterMain_ucGenConfig_ucConfigContainer_ucConfigPopup_cblSchemes_1
And so on, to 74.

Related

Selecting a webpage radio-button

I am trying to do some webpage data extraction using VBA within excel. I have managed to automate the login process, which takes me to a new page with a search form. This form has an input field for 'Search Value' and a series of radio buttons that specify the variable to conduct the search on eg. ID number, Name. I am trying to automate filling in the input field and selecting one of the radio buttons and then submitting the form.
The code I used to login does not seem to work on the next page where I need to do the search. This is the HTML from the search page containing the input field:
<table width="100%">
<tr>
<td style="font-weight: bold; width:10%;" nowrap="nowrap">Search Value</td>
<td style="width: 15%; text-align: left;">
<input name="txtSearch" type="text" id="txtSearch" onkeyup="return txtSearch_onkeyup()" style="background-color:White;border-color:#222244;border-width:1px;border-style:Solid;height:20px;width:125px;" />
Which I try to fill in using:
ieDoc.Document.getElementsByName("txtSearch")(0).Value = "Test String"
Or:
ieDoc.Document.all.txtSearch.Value = "Test String"
Or
ieDoc.Document.getElementByID("txtSearch").Value = "Test String"
...all giving me the same Object defined error.
I have confirmed that the ieDoc is referencing the correct page after the login (by checking the title in the immediate window), and tried to ensure it is not a timing issue with page-loading.
I know the HTML methods in vba tend to be temperamental but I've run out of ideas. Any other way to access the input field on this page?
Success!
I was trying to reference an element on a sub-frame within the IE object, which I accessed with:
ieDoc.Document.frames(0).Document.all.txtSearch.Value = "Hello!"

VBA web scrape innertext

I have now tried for quite some time to web scrape this innertext:
I want the value 0606 copied to an Excel sheet
<TABLE class="group"
<td width="100%" nowrap="" colspan="3">
<input name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityQualifier"
type="HIDDEN" value="CPR">CPR-nr:
<input name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue"
type="HIDDEN" value="0606">0606</td>
I have tried through get.attribute,getelementbyclassname, value and innertext, but now I need some fresh eyes on it.
Does any of you have a good idea?
Something like this should work, however without your code I don't know how you're obtaining your HTMLDocument:
Dim oHTMLDocument As Object
Dim ele As Object
Set oHTMLDocument = ... 'No code provided so I'm unsure how you obtained the HTMLDocument
For Each ele in oHTMLDocument.getElementsByTagName("input")
If ele.Name = "pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue" Then
Debug.Print ele.innerText
Exit For
End If
Next ele
You can use a CSS selector and avoid the need for a loop.
input[name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue"]
Try it
VBA
.querySelector is accessed via the HTML document set when you have your page (method not shown in your question) but with Internet Explorer for example:
IE.document.querySelector("pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue").innerText
Further info:
HTML DOM querySelector() Method

Loop in table with Watir

Stuck with watir trying to create a loop to click in all the links included in a table. Currently the table has this format:
<table id="test">
<tbody><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Link</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>http://facebook.com</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>http://google.com</td>
</tr>
</tbody></table>
And my current attempt looks like:
browser.table(:id => "test").rows do |tr|
tr.each do |td|
td.links.click
end
end
This code above does nothing in the browser & neither returns something in the terminal (no errors, no outputs).
Also tried a different approach using columns:
columns = browser.table(:id => "test").strings.transpose
browser.columns.each do |t|
t.click
browser.back
end
That outputs this error: jsfiddle.rb:24:in <main>': undefined methodcolumns' for # (NoMethodError)
The newer versions of Watir, version 6.0, automatically attempts to relocate stale elements. As a result, as long as the initial page does not change each time you reload it, you no longer need to save some sort of reference data before clicking the links.
It simply becomes:
my_table = browser.table(:id, 'test')
my_table.links.each do |link|
link.click
browser.back
end
This should work to click on each link in the table:
my_table = browser.table(:id, 'test')
table_links = my_table.links.map(&:text)
table_links.each do |link_text|
my_table.link(:text, link_text).click
browser.back
end
Largely based on Justin Ko's answer here.

how to tell if a url is for default blue image gravatar

Is there a way to tell if a url is for default blue gravatar image?
Here are two urls for the default Gravatar image:
http://www.gravatar.com/avatar/19b2990ba88512ab38abdbbca5701d27?s=120
http://www.gravatar.com/avatar/88a771dc1a611b2038c9a0ad0770b595?s=120
Here is a url that has an image:
http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?s=120
Is there something in these url's that I can search for that the default blue gravatar image url has that the non default gravatas do not have?
Edit:
What I am trying to do is:
I have an excel sheet downloaded from an app that has a column for gravatar urls. I need to delete all the links that go the the default blue gravatar image.
Gravatar avatar image URLs that are the default image will return a 404 Not Found error if the following parameter is set d=404. For example, here are the URLs that you used as examples, but with the parameter set:
http://www.gravatar.com/avatar/19b2990ba88512ab38abdbbca5701d27?s=120&d=404
http://www.gravatar.com/avatar/88a771dc1a611b2038c9a0ad0770b595?s=120&d=404
http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?s=120&d=404
Assuming you're detecting if the images default using JavaScript, you can then use AJAX (without displaying the image) or an error catcher (displaying non-default images) to detect if these images successfully loaded.
jQuery (AJAX)
// Image Exists
$.ajax({url:"http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?s=120&d=404",type:"GET",crossDomain:true,success:(function(){console.log("Custom Gravatar");}),error:(function(){console.log("Default Gravatar");})});
// Image Does Not Exist
$.ajax({url:"http://www.gravatar.com/avatar/88a771dc1a611b2038c9a0ad0770b595?s=120&d=404",type:"GET",crossDomain:true,success:(function(){console.log("Custom Gravatar");}),error:(function(){console.log("Default Gravatar");})});
Error Catching
You can use either the jQuery load and error event handlers, or the HTML onload and onerror attributes.
$("img").load(function(e) {
e.target.parentNode.parentNode.getElementsByClassName("stat")[0].innerHTML = e.type;
}).error(function(e) {
e.target.parentNode.parentNode.getElementsByClassName("stat")[0].innerHTML = e.type;
});
table,
td {
border: 1px solid black;
border-collapse: collapse;
}
table img {
width: 48px;
height: 48px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>Image</td>
<td>Expected Result</td>
<td>Actual Result</td>
</tr>
<tr>
<td>
<img id="good" src="http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?s=120&d=404" />
</td>
<td>load</td>
<td class="stat">Loading...</td>
</tr>
<tr>
<td>
<img id="bad" src="http://www.gravatar.com/avatar/88a771dc1a611b2038c9a0ad0770b595?s=120&d=404" />
</td>
<td>error</td>
<td class="stat">Loading...</td>
</tr>
</tbody>
</table>
EDIT: OP clarified what was being asked for
I wrote a small VBA script in this example file to iterate through the first column up until the first empty cell, creating WinHTTP requests with a modified URL then, as OP asked, deleting the contents of cells that contained a link to the default Gravatar avatar.
To run the code in the sample Excel file:
Excel 2003 and lower: Tools > Macro > Macros (Alt + F8) > checkGravatar
Excel 2007 and newer: Develooper > Macros > checkGravatar
In order to run the VBA, you may also need to enable macros and reference MSXML.
Sub checkGravatar()
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim URL As String
Dim goodStat As String
Dim badStat As String
Dim row As Integer
Dim pos As Integer
row = 1
URL = Cells(row, 1).Value
Do While Len(URL) > 0
If InStr(URL, "gravatar.com/avatar/") > 0 Then
If InStr(URL, "?") = 0 Then
URL = URL & "?d=404"
Else
If Not InStr(URL, "&d=") Then
URL = URL & "&d=404"
End If
End If
objHTTP.Open "GET", URL, False
objHTTP.send ("")
If objHTTP.Status = 404 Then
Cells(row, 1).Value = ""
Else
MsgBox "GET request failed"
End If
row = row + 1
URL = Cells(row, 1).Value
Loop
End Sub
You can try to pass a default image of your choice, and see what is returned.
URLs mapping to existing gravatars will ignore the default image parameter, returning an image.
URLs mapping do no images will use the default you provide (instead of the blue G).
If you pass something invalid (empty string), you'll get a 400.
Compare the existing one:
http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?d=%22%22
With the non-existing one:
http://www.gravatar.com/avatar/19b2990ba88512ab38abdbbca5701d27?d=%22%22
That said... It's quite rude to deliberately cause errors on someone else's API.
You could fire off some background worker that will GET the images and see what's returned.

Get the text of a link within a table cell

I have a table similar to this one:
<table id="space-list" class="aui list-container">
<tr class="space-list-item" data-spacekey="BLANKSPACEEXAMPLE">
<td class="entity-attribute space-name">
<a title="Blank Space Example" href="https://q-leap.atlassian.net/wiki/display/BLANKSPACEEXAMPLE/Blank+Space+Example+Home">
Blank Space Example
</a>
</td>
<td class="entity-attribute space-desc">
<span>
An example of a "Knowledge Base" type space, freely editable, accessible to everyone, may be deleted at any time.
</span>
</td>
</tr>
</table>
My PageObject code looks like this
class Space < PageObject::Elements::TableRow
def name
cell_element(index: 0).link_element(href: /q-leap/).text
end
def description
cell_element(index: 1).text
end
end
PageObject.register_widget :space, Space, :tr
class SpaceDirectoryPage
include PageObject
spaces(:space) do
table_element(:id => 'space-list')
.group_elements(:tag_name => 'tr')[1..-1]
end
end
And now I am iterating over all the rows in the table to get the content of each cell:
while true
on(SpaceDirectoryPage).space_elements.each_with_index do |space|
puts space.name
puts space.description
end
end
Which is working fine for the description, but I have no clue how to access the text of the link within the first column; tried 100s of things, nothing worked.
Thanks in advance!

Resources