Click the second button of an img in VBA - html

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

Related

VBA HTML Object Internet Explorer automation

How do I select an option from a dropdown menu and update the webpage.
Code:
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
then I navigate to a URL and I fetch the dropdown menu using html id and put value=my desired value
HTMLDoc.getElementById("xyz").Value = "dropdown_option"
I successfully get the option. But, the website doesn't update? e.g. filling up other fields.
I believe some javascript needs to be run.
How can i fix this using VBA?
Source code for dropdown menu field:
<div id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox" class="Invalid RadComboBox RadComboBox_Telerik SearchComboBox" style="width:206px;white-space:normal;">
<table summary="combobox" style="border-width:0;border-collapse:collapse;width:100%" class="rcbFocused rcbExpanded">
<tbody><tr class="rcbReadOnly">
<td class="rcbInputCell rcbInputCellLeft" style="width:100%;"><input name="ctl00$MainContent$CreateWebForm_$SubjectComboBox$ComboBox" type="text" class="rcbInput radPreventDecorate" id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_Input" value="Vælg" readonly="readonly" accesskey="s" autocomplete="off"></td><td class="rcbArrowCell rcbArrowCellRight"><a id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a></td>
</tr>
</tbody></table><input id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_ClientState" name="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_ClientState" type="hidden" autocomplete="off">
</div>
Source code for dropdown options:
<div id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Telerik " style="width: 204px; display: block; top: 0px; visibility: visible; transition: none 0s ease 0s;">
<div class="rcbScroll rcbWidth" style="height: 126px;">
<ul class="rcbList"><li class="rcbHovered">Forespørgsel til Energinet</li>
<li class="rcbItem">Måledata</li><li class="rcbItem">Målepunkt eller ad hoc spørgsmål</li>
<li class="rcbItem">Orientering til alle</li>
<li class="rcbItem">Priser</li><li class="rcbItem">Pristilknytninger</li></ul></div></div>
For anyone stuck with this issue. I solved this by finding focus property of getelementbyid method. Like this:
First I loop over the list to find my option. I use tagname and classname and matching the innertext property = myoption (This is only necessary if the field doesn't allow you to input text) Then when match occurs, perform .focus followed by .click on that object.
Otherwise just use focus followed by click property on the field and enter your option:
HTMLDoc.getElementById("xyz").focus
HTMLDoc.getElementById("xyz").click
HTMLDoc.getElementById("xyz").Value = myoption
This will automatically refresh the html page.
This solved my issues.

Click on Hyperlink for TD Object

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

Find element by xpath selenium web driver

I have a webpage:
<div class="formfonttitle">Wireless - General</div>
<div style="margin-left:5px;margin-top:10px;margin-bottom:10px"><img src="/images/New_ui/export/line_export.png"></div>
<div class="formfontdesc">Set up the wireless related information below.</div>
<table width="99%" border="1" align="center" cellpadding="4" cellspacing="0" id="WLgeneral" class="FormTable">
<tr id="wl_unit_field">
<th>Frequency</th>
<td>
<select name="wl_unit" class="input_option" onChange="_change_wl_unit(this.value);">
<option class="content_input_fd" value="0" >2.4GHz</option>
<option class="content_input_fd" value="1" selected>5GHz</option>
</select>
</td>
</tr>
I am trying to select "2.4GHz" using xpath. By default "5GHz" option is selected. I am doing this from Python script and using selenium webdriver.
I am doing this:
elements = mydriver.find_element_by_xpath("//div[#class='wl_unit']")
title = elements[1].find_elements_by_xpath(".//div[#class='content_input_fd']")
but it's not working.
You can use the following XPath to find the 2.4GHz option element, and then peform 'click' on the element to get it selected :
option = mydriver.find_element_by_xpath("//select[#name='wl_unit']/option[#value='0']")
option.click()
You can do this by running JavaScript with selinum like this
mydriver.execute_script("$('.input_option option[value="0"]')")
This may also help you for selecting an option in python selenium.
#Manoj
It is under select, so you can use select method which is generally used for dropdown as follows:
You wrote this.
Webelement elements = mydriver.find_element_by_xpath("//div[#class='wl_unit']")
Now continue :
Select object = new Select(elements);
object.selectByValue(1);
or
object.selectByVisibleText("5GHz")
Hope this will help you..
Try Following code
WebElement wlUnit= WebElement elea= driver.findElement(By.name("wl_unit"));
Select wlSelect = new Select(wlUnit);
wlSelect.selectByValue(0);
OR
WebElement wlUnit= WebElement elea= driver.findElement(By.name("wl_unit"));
Select wlSelect = new Select(wlUnit);
wlSelect.selectByVisibleText("2.4GHz");

How can i create xpath below this code

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.

Jquery Tabs refresh command adds too much

I'm trying to add new tab (runtime). The addTab function:
function addTab(name,url,ctx) {
var tabs = $("#tabs");
tabs.find( ".ui-tabs-nav" ).append('<li>Nazwa <span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></li>');
tabs.tabs("refresh");
}
The original code for tabs is:
<div id="tabs" class="tabs-bottom" style="background:url(/images/bg_document.png) top center no-repeat;">
<ul>
{foreach from=$master_structure item=item}
<li>{$item->nazwa}</li>
<li>{$item->nazwa}</li>
{/foreach}
</ul>
<div class="tabs-spacer" style="height:0px"></div>
</div>
(the params are not used, because i reduced code to sure values)
I'm very surprised, when I saw new second tab bar with totally new tabs.
It looks like all < li > tags are transformed to tabs.... (those from tab_index.html)
Is it a bug or i have to mark somehow other li tags to be ignored by refresh?
edited:
Now I know, that there is no problem with more < li> - in testing enviroment it works, so the problem has to be somewhere else... but still don't know where...
the source on jsFiddle: http://jsfiddle.net/Sledgehammer/hCdRy/, tab_index.html is:
<ul class="box-menu main-menu">
<li class="active">e-sklep</li>
</ul>
<table class="panel-table">
<tr>
<td><span style="cursor:pointer" onclick="$.addTab();">PRESS HERE TO ADD TAB</span></td>
</tr>
</table>
It was a bug! As you can see here:
http://bugs.jqueryui.com/ticket/9584