how to tell if a url is for default blue image gravatar - 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.

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!"

Excel VBA: Working with iFrame via IE Automation

I have a project that I am working on where I am trying to automate a site's behavior via Excel's VBA. So far, I know how to initialize a web browser from VBA, navigate to a website, and perform a simple task such as clicking on an item using the getElementById function and click method. However, I wanted to know how can I go about working with an embedded object(s) that is inside of an iframe.
For example, here is an overview of what the tree structure looks like via HTML source code. Of course, there are a lot more tags, but at least you can get an idea of what it is that I am trying to do.
<html>
<body>
<div>
<iframe class="abs0 hw100" scrolling="no" allowtransparency="true" id="Ifrm1568521068980" src="xxxxx" title="mailboxes - Microsoft Exchange" ldhdlr="1" cf="t" frameborder="0"> <<< ----- This is where I am stuck
<div>
<tbody>
<tr>
etc.....
etc.....
etc.....
</tr>
<tbody>
<div>
----------- close tags
I guess the biggest problem for me is to learn how to manipulate an embedded object(s) that is enclosed inside of an iframe because all of this is still new to me and I am not an expert in writing programs in VBA. Any help or guidance in the right direction will help me out a lot. Also, if you need more information or clarification, please let me know.
Here the code that I have written so far:
Option Explicit
'Added: Microsoft HTML Object Library
'Added: Microsoft Internet Controls
'Added: Global Variable
Dim URL As String
Dim iE As InternetExplorer
Sub Main()
'Declaring local variables
Dim objCollection As Object
Dim objElement As Object
Dim counterClock As Long
Dim iframeDoc As MSHTML.HTMLDocument 'this variable is from stackoverflow (https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba)
'Predefining URL
URL = ""
'Created InternetExplorer Object
Set iE = CreateObject("InternetExplorer.Application")
'Launching InternetExplorer and navigating to the URL.
With iE
.Visible = True
.Navigate URL
'Waiting for the site to load.
loadingSite
End With
'Navigating to the page I need help with that contains the iFrame structure.
iE.Document.getElementById("Menu_UsersGroups").Click
'Waiting for the site to load.
loadingSite
'Set iframeDoc = iE.frames("iframename").Document '<<-- this is where the error message happens: 438 - object doesn't support this property or method.
'The iFrame of the page does not have a name. Instead "Ifrm1" is the ID of the iFrame.
End Sub
'Created a function that will be used frequently.
Function loadingSite()
Application.StatusBar = URL & " is loading. Please wait..."
Do While iE.Busy = True Or iE.ReadyState <> 4: Debug.Print "loading": Loop
Application.StatusBar = URL & " Loaded."
End Function
Please note: My knowledge of programming in VBA is on an entry-level. So, please bear with me if I don't understand your answer the first time around. Plus, any nifty documentation or videos about this topic will help me a lot as well. Either way, I'm very determined to learn this language as it is becoming very fun and interesting to me especially when I can get a program to do exactly what it was designed to do. :)
You try to use the following code to get elements from the iframe:
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click
Sample code as below:
index page:
<input id="txtinput" type="text" /><br />
<input id="btnSayHello" type="button" value="Say Hello" onclick="document.getElementById('result').innerText = 'Hello ' + document.getElementById('txtinput').value" /><br />
<div id="result"></div><br />
<iframe width="500px" height="300px" src="vbaiframecontent.html">
</iframe>
vbaframeContent.html
<input id="txtcontentinput" type="text" /><br />
<input id="btncontentSayHello" type="button" value="Say Hello" onclick="document.getElementById('content_result').innerText = 'Hello ' + document.getElementById('txtcontentinput').value" /><br />
<div id="content_result"></div>
The VBA script as below:
Sub extractTablesData1()
'we define the essential variables
Dim IE As Object, Data As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("<your website url>")
While IE.ReadyState <> 4
DoEvents
Wend
Set Data = IE.Document.getElementsbyTagName("input")
'Navigating to the page I need help with that contains the iFrame structure.
IE.Document.getElementbyId("txtinput").Value = "AAA"
IE.Document.getElementbyId("btnSayHello").Click
'Waiting for the site to load.
'loadingSite
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click
End With
Set IE = Nothing
End Sub
After running the script, the result as below:

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.

mark all check boxes in internet explorer using excel vba

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.

Resources