VBA unable to get anchor element in table? - html

I'm trying to click an anchor element on a web page. I'm using this code:
For each anchor in HTMLDoc.anchors
If anchor.onclick = "validateForm(frmSearchEntry)" Then
anchor.click
exit for
End if
Next anchor
And here is the snippet of html:
<tr>
<td height="35" align="right" nowrap>
<input type="hidden" name="aisposm01formReset" value="">
<img src="/Settlement/static/images/pbs/funcreset.gif" border="0">
<img src="/Settlement/static/images/pbs/funcsearch.gif" border="0" alt=""></td>
</tr>
<tr>
<td valign="bottom" nowrap>
<table>
<tr><td name="txtErrorMessage" class="error"></td></tr>
</table>
</td>
</tr>
However, for some reason when I try to execute the code it just flies right over the for next loop as if there are no anchor elements anywhere on the page. I'm not quite sure what I'm doing wrong here.

Updated to reflect the answer the question noted in the comments
The elements you are trying to find are not actually anchors. See this link for a description of how anchors are implemented in html. An anchor looks like this <a name="anchor"></a> and you would create a link to it like so click here.
Instead, I you could use HTMLDoc.GetElementsByTagName("A") to get all of the links and then iterate through those. Make sure you perform your check based on the .getAttribute function as noted below.
The .OnClick function returns a pointer to the event handling function per msdn. What you probably want instead is .getAttribute(strAttributeName As String, Optional lFlags As Long) (msdn).
Try (UNTESTED):
For each linkelement in HTMLDoc.GetElementsByTagName("a") 'As noted above
If linkelement.getAttribute("OnClick") = "validateForm(frmSearchEntry)" Then
linkelement.click
exit for
End if
Next anchor
If that doesn't work right away, put a breakpoint on the If statement and check the value of anchor.getAttribute("OnClick") in the Watch Window.

Related

Nesting Div To Make Button Around Tel: Link?

I tried to nest a div before closing my tag to make a button around a tap to call tel: link:
<a href=”tel:+18888888888">
<div>
<table>
<tbody>
<tr>
<td rowspan="2"><img id="phone" src="/phoneimg.png" alt="xx"></td>
<td rowspan="2">Tap To Call NOW! 888.888.8888</td>
</tr>
</tbody>
</table>
</div>
</a>
When I upload the html file to my host I get a 404 error when I tap the button.
When I remove the div, and the closing tag is right behind the a href=”tel:+18888888888" tag (i.e. it has correct syntax without a div container before the closing tag) it works but is simply an undecorated link.
Any way to do this?
Thank you.
As dfionov correctly stated, you cannot include block elements inside inline elements. Anchors (<a>) are however an exception to the rule in HTML5, as explained in another answer.
I would like to also mention that for your use-case, tables should be avoided. They are for displaying tabular data not aligning an image.
There are a number of ways to do this with CSS, I would probably suggest using inline-block because it works on all modern browsers and is easier to understand than many of the alternatives.
<a href="tel:+18888888888">
<img id="phone" src="/phoneimg.png" alt="xx">
<span>Tap To Call NOW! 888.888.8888</span>
</a>
Corrected code:
<div>
<table>
<tbody>
<tr>
<td rowspan="2"><img id="phone" src="/phoneimg.png" alt="xx"></td>
<td rowspan="2">Tap To Call NOW! 888.888.8888</td>
</tr>
</tbody>
</table>
</div>

Jsoup How can i parse element which show after click button (image)?

I have this div in source of website, and after click it shows more data.
<div class=infogroup>
<div onClick=requestAjax(); style=cursor:pointer; class=infoheading>Your Panels (Click to load results)<img style=float:right; src="/images/plus_sign.png" width=20 height=20></div>
<div id=resultsPanels style=display:none;>
<table width=100%>
<tr>
<td>
<center>Loading your results...</center>
</td>
</tr>
<tr>
<td>
<center><img src=/images/ajaxloader.gif></center>
</td>
</tr>
</table>
</div>
</div>
when i click it and do "inspect element" witch chrom i can see it in source website
when i copy xpatch the xpatch of interesting field is:
//*[#id="resultsPanels"]/div/table/tbody/tr[3]/td[3]
how do i select it in Jsoup ?
i tried like this but it isnt working:
elements = doc.select("//*[#id=\"resultsPanels\"]/div/table/tbody/tr[2]/td[3]");
this is what mozzila do when i click button:
Jsoup doesn't support javascript / ajax, but you can use an external library for that (i've listed some examples here).
Btw. is using CSS / jQuery selector syntax, not XPath. So this syntax will likely fail.
You can get an overview about Jsoups selector here: Use selector-syntax to find elements

VBA Interacting With Radio Buttons On Webpage

EDIT: A few months later I attempted this again using the same syntax I had tried the first time (posted below). For some reason, it worked! Maybe something other than my syntax was causing the ineffectiveness... END EDIT
I've been searching forums for a couple of hours now trying to find a solution to this problem, but none of the things I've tried work.
I'm using VBA to automate the process of creating a survey on SurveyMonkey. So far I've been able to:
Log into my account,
Click several hyperlinks to create a new response collector,
Name the collector,
Go to the collector settings,
And, select three of four radio buttons to change collector settings.
The issue isn't that I can't select the radio buttons; my code selects the first three buttons just fine. What puzzles me is that the fourth button doesn't change! I use the same process for each button, so I can't figure out why the last button won't select.
Here's the section of my code:
objIE.Document.getElementById("rdlResponseType_1").Click 'Allow multiple responses = Yes
objIE.Document.getElementById("rdlResponseEdit_1").Click 'Allow Responses to be Edited = Yes
objIE.Document.getElementById("rdlThankyou_1").Click 'Display a "Thank You" page? = Yes
objIE.Document.getElementById("rdlCompleteOpt_1").Click 'Survey Completion = Close Window
This is the HTML for the radio buttons:
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
<tr id="CompleteOptDesc">
<td style=""> </td>
<td>After the respondent leaves the survey:</td>
</tr>
<tr id="CompleteOptItems">
<td style=""> </td>
<td align="left" nowrap="nowrap" valign="top">
<table id="rdlCompleteOpt" class="Clean radioList" OnClick="radioToggle('rdlCompleteOpt', '0', 'panLink');" name="rdlCompleteOpt" border="0" style="white-space: nowrap">
<tr>
<td><span style="white-space: nowrap"><input id="rdlCompleteOpt_0" type="radio" name="rdlCompleteOpt" value="0" checked="checked" /><label for="rdlCompleteOpt_0"><b>Redirect</b> to your own webpage.</label></span></td>
</tr><tr>
<td><input id="rdlCompleteOpt_1" type="radio" name="rdlCompleteOpt" value="2" /><label for="rdlCompleteOpt_1"><b>Close Window</b></label></td>
</tr>
</table>
Here is the code for the toggle section of the radio:
<td width="65%" valign="top">
<div id="panLink" style="DISPLAY:inline">
<div class="URLinfo">
<b>Enter a URL</b> to jump to upon leaving the survey:<br />
<div title="REQUIRED: Enter URL (255 character max)" style="padding:4px 0 2px 0;">
<input name="txtWebLink" type="text" value="http://www.surveymonkey.com/" maxlength="255" size="40" id="txtWebLink" class="RQR opaque" />
</div>
<span class="tip">Example: <u>http://www.mysite.com/home.html</u></span>
</div>
</div>
</td>
Any suggestions would be very appreciated!
There's a couple things to consider:
When are you calling this code? OnNavigate or DocumentComplete? It may be that the object doesn't exist yet. You can check to see if it does by using a breakpoint and adding a watch for the expressions
objIE.Document.getElementById("rdlCompleteOpt_1")
Use OnDocumentComplete to ensure the HTML components exist
It's possible the way you are calling the click function is causing an issue. Try this
Call objIE.Document.getElementById("rdlCompleteOpt_1").Click()
I doubt that it is likely to have much impact but I like to verify everything.
Isolate the cause of the issue by removing the 'layers'. Can you accomplish this using pure JavaScript that's invoked from the IE dev tools (F12) JavaScript console?
If no then the issue is with how you're interacting with the page elements. If yes then we need to figure out what it is with the VBA intaction is causing an issue. Again see #2 as it is likely the cause of the issue.
Also, does the issue occur if you change the HTML order of the elements? What if you call it this way?
objIE.Document.getElementById("rdlCompleteOpt_1").Click 'Survey Completion = Close Window
objIE.Document.getElementById("rdlResponseType_1").Click 'Allow multiple responses = Yes
objIE.Document.getElementById("rdlResponseEdit_1").Click 'Allow Responses to be Edited = Yes
objIE.Document.getElementById("rdlThankyou_1").Click 'Display a "Thank You" page? = Yes
Does it work then or does the "last" item fail?
Highly unlikely to be the cause but the HTML supplies is missing the code to close cell, row and table.
</tr>
</table>
<!-- missing -->
</td>
</tr>
</table>
While not the specific answer to your question I believe this may be inline with the intent of your post. Since you want to manage a surveymonkey collector take a look at the SurveyMonkey API here as using an API gives you quite a few benefits.
you don't have to rely on user elements which may change or be changed outside of your control
you can use a controlled and "contracted" interface to manage the information which covers you for any future changes (assuming they don't deprecate and stop supporting the API)

SharePoint 2010 >> Editing Output HTML >> Issues with page or master page render override

Ok. This is a big one. I'll try and explain my issue but let me know if you need more info.
I would like to alter the HTML that SharePoint 2010 generates.
I'm going to use the HTML Agility Pack] which will take a string of HTML among other objects and alter the source.
There are 2 ways of altering the full source in SP.
Using Control Adapters, or Extending Controls I can access the Page, MasterPage or even ContentPlaceHolder render methods, grab the HTML, Alter it and then write it.
Use a HTTP Module with a filter and alter the output stream
Unfortunately there are issues with both of these methods.
Number 2, the filter works good but you have to disable the Output Cache.
I can't do this. The sites I brand have massive amounts of traffic.
So filters are moot until the SharePoint 2010 team fixes / presents us with a workaround. I read somewhere in my travels they are aware and will be doing something about it.
Number 1 works great. I simply use the following and I can alter the HTML of the page but there is one big problem.
HtmlDocument hd = new HtmlDocument(); //Agility HTML Object.
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
//Render Object into HtmlTextWriter
base.Render(htw);
//String to hold the HTML from the StringBuilder of the HtmlTextWriter
string html = sb.ToString();
//Mess with the String here using the Agility HTML Pack
//Write the HTML to the writer
writer.Write(html);
//Done!
This code works great, but SharePoint 2010 is appending data to the writer / or altering controls after the render override for the Page / MasterPage.
When i debug and walk-through and look at the html string it look like the following.
"<html .... ....</html>"
This is important to note because there is nothing before the first HTML tag. The Opening "<" is in position 0.
But when the HTML ends up in the browser I see the following.
DOMAIN\user<script type="text/javascript">
//<![CDATA[
var _spUserId=1;
//]]>
</script>
<html...
....</html>
Also the user name is missing from the welcome area in the top right of the ribbon because it's now putting it up before the opening tag.
If I don't assign the writer object anything in the render override the page in the browser just shows everything before the HTML tag. The funny thing is it shouldn't show anything!
If I visit other pages. ANY page that has a list view. All of the items are rendered before tag. It's important to note that it's just the Values, not the html. The HTML for the list items exists where it should down in the list view.
For Example. (Page is "/_layouts/viewlsts.aspx" )
Without the render override.
<html...
...
//The Table where the data should be.
<tr>
<td class="ms-gb" colspan="5" style="white-space:nowrap;">
<h3 class="ms-standardheader">
  Picture Libraries
</h3>
</td>
</tr>
<tr><td class="ms-vb2 ms-viewlsts-noitems" colspan="6">
There are no picture libraries. To create one, click <b>Create</b> above.
</td></tr>
<tr>
<td class="ms-gb" colspan="5" style="white-space:nowrap;">
<h3 class="ms-standardheader">
  Lists
</h3>
</td>
</tr>
<tr><td class="ms-vb2 ms-viewlsts-noitems" colspan="6">
There are no lists. To create one, click <b>Create</b> above.
</td></tr>
<tr>
<td class="ms-gb" colspan="5" style="white-space:nowrap;">
<h3 class="ms-standardheader">
  Discussion Boards
</h3>
</td>
</tr>
<tr class="ms-alternatingstrong">
<td class="ms-vb-icon">
<a id="viewlistDiscussionBoard" href="/Lists/Discussion%20Board/AllItems.aspx" >
<img border="0" alt="Discussion Board" src="/_layouts/images/itdisc.png" width="16" height="16" /></a>
</td>
<td class="ms-vb2" >
<a id="viewlistDiscussionBoard" href="/Lists/Discussion%20Board/AllItems.aspx">Discussion Board</a> 
</td>
<td class="ms-vb2" width="40%" >
 
</td>
<td class="ms-vb2" width="3%" align="right">
1
</td>
<td class="ms-vb2" width="25%" >
<nobr>
3 days ago
</nobr>
</td>
</tr>
...</html>
With the render override.
DOMAIN\user<script type="text/javascript">
//<![CDATA[
var _spUserId=1;
//]]>
</script>Document Libraries"viewlistDocumentLibrary""/AnalyticsReports/Forms/AllItems.aspx""Customized Reports""/_layouts/images/itdl.png""viewlistDocumentLibrary""/AnalyticsReports/Forms/AllItems.aspx"Customized ReportsThis Document library has the templates to create Web Analytics custom reports for this site collection04 days ago"viewlistDocumentLibrary""/Style%20Library/Forms/AllItems.aspx""Style Library""/_layouts/images/itdl.png""viewlistDocumentLibrary""/Style%20Library/Forms/AllItems.aspx"Style LibraryUse the style library to store style sheets, such as CSS or XSL files. The style sheets in this gallery can be used by this site or any of its subsites.04 days agoPicture LibrariesThere are no picture libraries. To create one, click <b>Create</b> above.ListsThere are no lists. To create one, click <b>Create</b> above.Discussion Boards"viewlistDiscussionBoard""/Lists/Discussion%20Board/AllItems.aspx""Discussion Board""/_layouts/images/itdisc.png""viewlistDiscussionBoard""/Lists/Discussion%20Board/AllItems.aspx"Discussion Board13 days agoSurveysThere are no surveys. To create one, click <b>Create</b> above.Blog0
<html...
//The Table where the data should be.
...
<tr>
<td class="ms-gb" colspan="5" style="white-space:nowrap;">
<h3 class="ms-standardheader">
 
</h3>
</td>
</tr>
<tr><td class="ms-vb2 ms-viewlsts-noitems" colspan="6">
</td></tr>
<tr>
<td class="ms-gb" colspan="5" style="white-space:nowrap;">
<h3 class="ms-standardheader">
 
</h3>
</td>
</tr>
<tr class="ms-alternatingstrong">
<td class="ms-vb-icon">
<a id= href= >
<img border="0" alt= src= width="16" height="16" /></a>
</td>
<td class="ms-vb2" >
<a id= href=></a> 
</td>
<td class="ms-vb2" width="40%" >
 
</td>
<td class="ms-vb2" width="3%" align="right">
</td>
<td class="ms-vb2" width="25%" >
<nobr>
</nobr>
</td>
</tr>
... </html>
My guess is it has something to do with resource files..
Whatever the case it's obvious that there is some sort of manipulation of the objects after the Page or MasterPage render methods. I just can't find it.
My HTTP Module filter stream has the HTML in the proper place..
So somewhere between the Page render and sending the HTML to the browser there is something going on.
Here are a couple of other people reporting the same issue without any useful responses.
Link 1
Link 2
I would appreciate any insight on this! THANKS!
The problem is caused by something in the page interfering with the outputcache during render, and more specifically the behaviour of the PostCacheSubstitutionTextHelper class. Probably your call to Render above triggers this.
The welcome.asxc control behaves approximately like this (pseudo sequence diagram):
Page Render > Welcome.ascx Render > PersonalActions Render > PostCacheSubstitutionText.Render > PostCacheSubstitutionTextHelper.RenderAndRegisterSubstitutionCallbackHandler
(new instance of)PostCacheSubstitutionText.Render(called in delegate and now writes to HtmlTextWriter) > PostCacheSubstitutionTextHelper. RenderAndRegisterSubstitutionCallbackHandler HttpContext.Response.WriteSubstitution(stuffFromNewInstanceOfPostCacheSubstitutionText)
So enough with the sharepoint freakshow and over to the workaround:-)
Try adding this to your welcome.ascx in the hive:
// in bottom of directives in /CONTROLTEMPLATES/Welcome.ascx
<%# OutputCache Duration="1" VaryByParam="none" %>
Notice that the Duration is set to 1 since 0 is not allowed for user controls. this leaves a theoretical possiblity for failure, but in our scenario it worked.
I ran into this same problem, I was able to workaround the issue by removing this line from the masterpage.
<wssuc:Welcome id="IdWelcome" runat="server">
</wssuc:Welcome>

How do I grab info from source code from other site?

Is there a way to grab things out of the source code of another site directly into your site?
For example, let's say than in a site the following source code exists:
<table ...>
<tr>
<td class=...>...</div></td>
</tr>
<tr>
<td class=....><div align="... class=...>"Interesting string that keeps changing"</div></td>
</tr>
</table>
And we want that Interesting string that keeps changing to appear in our website as well.
you could use php
you use
$html = file_get_contents('url to website');
or use a hidden if you want a javascript function, and then just grab the innerhtml