How to click Save button on a web form using VBA - excel

I am working on automating a task. I want to click a Save button on a web form using VBA, but it's not working:
<input name="save" title="Save" class="btn" type="submit" value=" Save ">
<input name="save" tabindex="79" title="Save" class="btn" type="submit" value=" Save ">
I've tried ie.Document.all("save").Click, but it doesn't seem to work. What method do I need to click the button?

You can try going through all your "btn" class collection, and click the one with your save value:
Dim btnClassColl As Object, btn As Object
Set btnClassColl = ie.document.getElementsByClassName("btn")
For Each btn In btnClassColl
If btn.Value Like "*save*" Then
btn.Click
Exit For
End If
Next
Also: make sure that your web page has FULLY loaded before trying to automate anything.
Edit
In response to the comment:
This code is neither giving error nor its clicking on btn. Can this be because there are two buttons on web page with same name and function?
An alternative solution would be that if you know the index number of the collection item, you can simply use that index number and not loop at all. In this case, your index # is 1 (remember: Base 0).
Try this alternative:
Dim btn As Object
ie.document.getElementsByName("save")(1) '1 actually means #2 in index

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

How can I click a button on web page using VBA?

URL-
<input type="image" src="/live_market/resources/images/gobtn.gif" alt="btn" onclick="goBtnClick('stock');" style="cursor: pointer">
I am trying to click the go button.
IE.document.getElementById("underlyStock").Value = "ACC"
IE.document.all.Iteam(stock).FireEvent ("onclick") ' not working
IE.document.querySelector("goBtnClick('stock')").Click 'not working
The selector you want is probably:
input[type=image][onclick]
You can invoke the onclick function directly:
document.querySelector("input[type=image][onclick]").onclick()

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

What DOM element to specify with a Directive?

I'm using Soda (node.js) w/ Selenium RC to automate browser testing.
If I have an AngularJS directive -> say a button that has a spinner -> and it appears multiple times on a page, how can I make sure to click on that particular button...when it has no DOM ID or unique class? In my case, "Login" and "Some Other Button" are dynamic and would be unique for all buttons in the ng-view.
<my-requesting-button text="Login" class="ng-isolate-scope ng-scope">
<button type="submit" class="btn btn-large">
<img src="/img/progress.gif" style="display: none;">
<span>Login</span>
</button>
</requesting-button>
<my-requesting-button text="Some Other Button" class="ng-isolate-scope ng-scope">
<button type="submit" class="btn btn-large">
<img src="/img/progress.gif" style="display: none;">
<span>Some Other Button</span>
</button>
</requesting-button>
Soda (basically directly from the example):
browser
.chain
.session()
.open('/')
.clickAndWait('...LOGIN BUTTON REF?')
.waitForPageToLoad(2 * 1000)
.clickAndWait('...SOMEOTHER BUTTON REF?')
.waitForPageToLoad(2 * 1000)
You can give it a unique ID/class name. If you know this element will only ever be on the page once, you can give it a unique ID. If not, you can give it a class name so that Selenium can find just the first one, or the one near your login form.
To do this, you can either pass the text value (e.g. "Login") to your button so that it has it:
<button class="{{text}}Button">
or you can tell Selenium to find the button that comes right after the element whose text attribute is "Login".

How to click a edit button when its defined under repeated similar ids

I have a page where many edit buttons are there. And each button have same id i.e enable_edit_content . How to click a specific button .
This is the code where i have to click
<div class="sub-controls" id="motion_eligibility_entry-subcontrols">
<button class="btnedit" **id="enable_edit_content"** name="button" type="button"></button>
<input class="btnsave submit_form" id="save_motions_eligibility_entry" name="commit"
type="submit" value="" disabled="disabled">
<button class="btnkill" id="cancel_content" name="button" type="button"
disabled="disabled"></button>
</div>
Any help will be highly appreciated.
I have tried but could not get my results :-
1) page.all(:css, '#enable_edit_content').each_with_index do |el, i|
i += 1
if i == 3
el.click
end
end
2)find(:xpath, "//div[#id='motion_eligibility_entry-subcontrols']/button[1]").click
you can also scope down your css selector with a within block.
within('.sub-controls) do
page.find('#enable_edit_content).click
end
Because of this very issue, I'd highly recommend shying away from xpath and use css selectors instead!
Hope this helps.

Resources