I want to click on a hyperlink on a webpage using VBA that I have found using the below method:
Set trs = ie.document.getElementsByTagName("tr")
For Each trObj In trs
Set tds = trObj.getElementsByTagName("td")
For Each tdObj In tds
If tdObj.className = "btn_container" And tdObj.innerText = "Reinstate Award" Then
tdObj.Click
End If
Next
Next
I can't get the tdObj.Click bit to click the hyperlink.
The html is below:
<td class="btn_container">
<input name="_linkSubmit" type="hidden"><a name="assessment_Reinstate" class="button_link" id="assessment_Reinstate" onclick="javascript:return disableLinks(this.href);" href='javascript:submitLink(document.Form0,"assessment_Reinstate");' type="button" renderer="uk.co.slc.tapestry.link.PortalLinkRenderer#1a79ffb">Reinstate Award</a>
</td>
Any help would be greatly appreciated.
Thanks
Jim
You can try using the link id
ie.document.querySelector("#assessment_Reinstate").click
Related
So I have an html form created in classic asp that has about 25 check boxes with different labels. The checkboxes are written to the page via a loop that grabs the label values from a table and each checkbox is named similarly ("decreason1","decreason2","decreason3",ect...) using a loop variable.
I use a vbscript at the end of the page to validate the data on the page before passing with a submit button. I need to make sure that at least one of the checkboxes is checked before submitting the form.
dim i, checkedvalue ;
checkedvalue = false
for i = 1 to 25
if document.rtftadd.decreason1.checked = true then
checkedvalue = true
end if
next
I need the "document.rtftadd.decreason1.checked" to change with the loop to decreason2, decreason3
I'm not sure how to go about doing that.
Any help would be greatly appreciated.
There are a few different ways to do this and if you are seeking a validation method prior to form submission then JavaScript may be a better solution.
However, in an effort to address your question using only ASP Server side code for VBScript and considering your question suggest that you do not care which box is selected nor how many are selected then the following code may be a working solution for you.
By looking at all of the elements sent via the form and checking if your form name (without number) exist then this same block of code will work even if you increase or decrease the number of check boxes you are validating for input.
<%#LANGUAGE="VBSCRIPT"%>
<%
dim checkedvalue
checkedvalue = false
if request("btn1") = "Submit" then
For each item in request.form
if instr(item,"decreason") > 0 then
checkedvalue = true
end if
next
response.write "Checked Value = " & checkedvalue & "<br><br>"
end if
%>
<html>
<head></head>
<body>
<form method="post" action="index.asp" name="rtftadd">
<input type="checkbox" name="decreason1"> Box 1<br>
<input type="checkbox" name="decreason2"> Box 2<br>
<input type="checkbox" name="decreason3"> Box 3<br>
<input type="checkbox" name="decreason4"> Box 4<br>
<input type="checkbox" name="decreason5"> Box 5<br>
<br>
<input type="submit" value="Submit" name="btn1">
</form>
</body>
</html>
So I have the following HTML code (I'm not super familiar with HTML so excuse the incorrect spacing):
<div class="section_heading">
<span class="section_anchor" id="ajax_result_table_link" data-label="vs. Pitchers"></span>
<h2>vs. Pitchers</h2>
<div class="section_heading_text">
<ul>
<i>Sorting only sorts the values in the table shown</li>
<li>Click a player to see each PA</li>
<li class="hasmore drophover">
<span href="javascript:void(0)">Share & more</span>
<div>
<div class="closer" role="button" aria-label="close this menu"></div>
<ul>
<li><button class="tooltip" id="share_on_ajax_result_table" tip="Use a customizable report creator that can<br>output HTML, CSV, or a shareable link.">Modify & Share Table</button>
</li>
<li><button class="tooltip" tip="Get a bit of widget code to emed this table on your site">Embed this Table</button></li>
<li><button class="tooltip" tip="Convert the table below to comma-separated values<br>suitable for use with excel">Get as Excel Workbook (experimental)</button><a id="dlink" style="display: none;"></a></li>
<li><button class="tooltip" tip="Export table as <br>suitable for use with excel">Get table as CSV (for Excel)</button></li><li><button class="tooltip" tip="">Strip Mobile Formatting</button></li>
From this URL: https://www.baseball-reference.com/play-index/batter_vs_pitcher.cgi?batter=mancitr01
I am trying to use VBA to download the Excel workbook (under the "Share & More" Dropdown), but I can't seem to be able to click the link. I've tried using get element by class and get element by ID but they don't seem to work. I'm guessing this HTML code is a little more advanced than I'm used to. I've been trying to click the ID "dlink" but I'm not sure if that is the end goal.
Any help or input would be appreciated.
EDIT:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.baseball-reference.com/play-index/batter_vs_pitcher.cgi?batter=mancitr01"
Wait 10
IE.document.getElementById("dlink").Click
Exit Sub
Set oElementCollection = IE.document.getElementsByClassName("section_heading_text")
For Each span In oElementCollection
If span.innerHTML = "Share & more" Then
span.ParentNode.Click
Exit For
End If
Next span
Exit Sub
I'm trying to click an imagebutton on IE by using getAttribute("src"), but there are to identical buttons with the same src-code, so when I use this code, it clicks the first and I want to click the second. Can somebody help me?
I'm thinking of creating a loop to search through and choose the second one, but I have no idea how to write the code! Can somebody help me out?
This is the code I use to click the first button:
Set allInputs = IE.Document.getElementsByTagName("img")
For Each Element In allInputs
If Element.getAttribute("src") = "/forms/Content/Images/0006.gif" Then
Element.Click
Exit For
End If
Next Element
The HTML code for the buttons are:
<tr class="jqgfirstrow" role="row"
<tr tabindex="-1" class="ui-widget-content jqgrow ui-row-ltr" id="1" role="row"
< img src="/forms/Content/Images/0006.gif"
<tr tabindex="-1" class="ui-widget-content jqgrow ui-row-ltr" id="2" role="row
< img src="/forms/Content/Images/0006.gif"
Thanks for all of your help!
I did it with code here:
Set allInputs = IE.Document.getElementsByTagName("img")
allInputs(2).click
I am looking at an HTML code link below:
<h1 class="wer wer">
<a href="http://somelink.com" rel="bookmark" title="Permanent Link to Title of this page that covers some random topic">
Short title of this page...</a>
</h1>
I am currently using the below code to pull out innertext ("Short title of this page...")
For Each ele In .document.all
Select Case ele.classname
Case "wer wer"
RowCount = RowCount + 1
sht.Range("A" & RowCount) = ele.innertext
End Select
Next ele
How can I modify this code to pull out title ("Permanent Link to Title of this page that covers some random topic") and href ("http://somelink.com")?
Any help would be much appreciated. Thanks.
Select the element by its styling.
.document.querySelector("a[href=http://somelink.com]").innerText
a[href=http://somelink.com] is a CSS selector of first element with an a tag having href = 'http://somelink.com'.
I am trying to create xpath this (Unscheduled Visitor Check-In) button. i am unable to find the button name or id. can anyone help me out from this issue please. will appreciate that. please see the attachment.
<div id="SAFEControl307369" class="buttontextactivestyle" uniquefieldid="307369" onselectstart="return false" onchange="setAttribute('IsModified','1');" onclick=" document.body.style.cursor = 'wait'; window.location.href = 'QSForm.aspx?FormID=194&Hidden=1';return false;" datatype="" boundcolumn="" ismodified="0" style="position:absolute;left:280px;top:5px;height:0px;width:250px; ;" caption="" tabindex="-1">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr style="height:20px;cursor:pointer;">
<td class="buttonleftactivestyle"/>
<td class="buttonmiddleactivestyle" align="center">
<div>Unscheduled Visitor Check-In</div>
[Screenshot of the application with xpath][1]
IWebElement element = driver.FindElement(By.XPath("//div[#id='SAFEControl307369']/table/tbody/div"));
When you checking the element you need look on the try for create the XPath.
Please use below xpath:
//div[contains(.,'Unscheduled Visitor Check-In')]
You can try the code below:
WebElement element= driver.findElement(By.xpath("Your XPath"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Feel free to change the locator in the code above. Try to with xpath provided by the firepath as shown in image.