Results 1 to 15 of 15

Thread: [RESOLVED] VBA IE Problem Selecting Radio Button

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Resolved [RESOLVED] VBA IE Problem Selecting Radio Button

    Hi Guys,

    I am having a little trouble selecting a radio button on a webpage that has...alot of javascript fuctions. On a normal webpage I would execute the following codes:
    Code:
    IE.document.getElementById("selectedvariable")(11011).Checked = True
    or the following:
    Code:
    Set radio_button = IE.document.all.Item("SelectedVariable") 
    For Each rb In radio_button 
    If rb.Value = "808725" Then 
    rb.Checked = True 
    End If
    Lately I have been using a point and click program to help me identify form names, tag names, and values quicker. The program has genereate the following vb.net string which selects the radio button:

    Code:
    scene.HtmlInputRadioButton("TargetSelected").Invoke("Click", ControlActionType.InvokeMethod, "Target -2644426-2644366")
    The HTML source code that I can find:

    Code:
     Input name="TargetSelected"  onclick="setSelectedTarget(this)" type="radio" value="Target-2644426-2644366"/>
    The website has a bunch of information that I have to fill in. The Radio button comes from a pop up window( it is not a new browser window just a script window that populate with data from the parent tree. kind of hard to explain) The pop window is executed by the following code:

    Code:
    Call IE.document.parentWindow.execScript("openSearchWindow('search')")
    I have tried both of my methods listed above and both have failed.
    I am having a little difficulting following all of the html but If I can provide any additional script please let me know.
    I am open to any and all suggestion. Thanks again in advance.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA IE Problem Selecting Radio Button

    it is not a new browser window just a script window
    it may be a frame or iframe, which in turn can host a separate document, check if the frames.length change when the search window is opened
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: VBA IE Problem Selecting Radio Button

    from what i can find it posts to a form:

    HTML Code:
    <form name="TargetForm" action="/tcf/Target.view" method="post">
    which is part of an iFrame:

    HTML Code:
    <iframe align="center" id="newTargetSearchFrame" src="/tcf/Target.view?action=searchTarget&TargetSearchCriteria=TargetName&TargetSearchString=*&reportType=QueryToolGeneral" frameBorder="0" noResize="noresize" scrolling="no" style="z-index: 1; position: absolute; width: 797px; height: 401px; top: 215px; left: 231px;" valign="middle">

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA IE Problem Selecting Radio Button

    if the form is within an iframe, try like
    vb Code:
    1. set fr = ie.document.getelementbyid("newtargetsearchframe")
    2. fr.document.forms("targetform").getElementById("selectedvariable")(11011).Checked = True
    i did not test this, but should give an idea
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: VBA IE Problem Selecting Radio Button

    Great. I am now able to select the radio button but when i submit the form it does not recognize that the button has been clicked. Here is my code:

    Code:
    Set Frame = appIE.document.getElementById("newTargetSearchFrame")
    Set selection = Frame.contentWindow.document.getElementById("TargetSelected")
    selection.Value = "Target - 5675309 - 5675309"
    selection.Checked = True
    I have been looking into the source and noticed that the following javascript:

    Code:
    function setSelectedTarget(checkBox) {        	
                checkBoxChecked = true;
                var selectedTargetNameAndId = replaceStringWithDoubleQuotes(checkBox.value);            
                document.TargetForm.selectedTargetNameAndId.value = selectedTargetNameAndId;
    I am dont really know javascript but I do know I have executed javascript in the past with the following code:

    Code:
    Call appIE.document.parentWindow.execScript("openSearchWindow('search')")
    However I dont know how to execute that setSelectedTarget(checkBox) fuction. Any Ideas????
    Last edited by sskicker23; Mar 16th, 2011 at 08:12 AM.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA IE Problem Selecting Radio Button

    as selection is a built-in range object for most applications, do not use it for a variable

    you can use the fireevent method of any element, by passing the event the code is under, most often onclick or onblur, i see from above it is onclick,

    vb Code:
    1. selectedelement.fireevent "onclick", this
    this being the form element that contains the input elements, or the element that has the code (selecteditem)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: VBA IE Problem Selecting Radio Button

    Perfect :

    Code:
    Set Frame = appIE.document.getElementById("newTargetSearchFrame")
    Set SubFrame = Frame.contentWindow.document.getElementById("TargetSelected")
    SubFrame.Value = "Target - 5675309 - 5675309"
    SubFrame.FireEvent "onclick", this
    I am sure there is a way to condense it and I will research it later...... However I am having one last trouble and that is submitting.

    Source:
    Code:
     
    <!-- 'Submit' button code starts -->
    <td width = "33" height = "28">
    			
    <table border="0" cellpadding="0" cellspacing="0"
    onclick="callParentSubmiText();parent.closeSearchWindow(checkBoxChecked);return false;" title='Submit'>
    <tr>
    <td class="btnLeft"><img border="0" src="/tcf/common/images/site/spacer.gif" width="10" height="1"></td>
    							
    <td class="btnMiddle"><a href="#" class="buttonLink">Submit</a></td>
    							
    <td class="btnRight"><img border="0" src="/tcf/common/images/site/spacer.gif" width="10" height="1"></td>
    </tr>
    </table>
    </td>
    <!-- 'Submit' button code ends -->
    Javascript for callParentSubmiText() fuction:
    Code:
     function callParentSubmiText() {
                if(!checkBoxChecked) {
                    document.getElementById("errorSelectTarget").innerHTML = replaceStringWithDoubleQuotes("Select one Target");
                    return;
                }            
                var id = document.TargetrForm.selectedTargetNameAndId.value;
                parent.displaySelectedTarget(id);
            }
    I have tried to click the button using the anchor tag but I get an object variable error. However, I use the same code to click other buttons.....any suggestions?

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA IE Problem Selecting Radio Button

    you could try
    vb Code:
    1. appie.document.forms("targetform").submit
    check i got form name right
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: VBA IE Problem Selecting Radio Button

    Just gave it a try.

    I get a runtime error 91 object variable or with block variable not set.

    Is there any additional data I should be looking for?

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA IE Problem Selecting Radio Button

    Is there any additional data I should be looking for?
    the iframe that contains the form, probably your subframe object
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: VBA IE Problem Selecting Radio Button

    The Iframes name is newTargetSearchFrame.

    it has a form within the iframe that is TargetForm

    I have the xpath to the anchor:

    Code:
    HTML[1]/BODY[1]/TABLE[1]/TBODY[1]/FORM[1]/TR[1]/TD[2]/TABLE[1]/TBODY[1]/TR[8]/TD[1]/TABLE[1]/TBODY[1]/TR[1]/TD[2]/TABLE[1]/TBODY[1]/TR[1]/TD[2]/A[1]
    I just dont dont know how to utilize the xpath in vba.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA IE Problem Selecting Radio Button

    try like
    vb Code:
    1. subframe.document.forms("targetform").submit
    within the code in post #7
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: VBA IE Problem Selecting Radio Button

    I dont get it. Running the following code:

    Code:
    subframe.document.forms("TargetForm").submit
    I got a permission denied error

    I then tried to use the send keys just to give it a try and I was able to tab over to the submit button. I then used send keys to press enter and no action. However when I physically push the enter key after tabbing over it works.

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA IE Problem Selecting Radio Button

    using sendkeys now is not really acceptable, as it will not work at all with my w7 installation (permission denied error)


    you can try calling the javescript function or using fireevent for the submit element

    without testing, on the webpage, i can offer little further asistance
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: VBA IE Problem Selecting Radio Button

    Got it
    Code:
    Set htm = appIE.document.Frames("newTargetSearchFrame").document
    Set frms = htm.forms("TargetForm")
    Set Class1 = frms.document.getElementsByTagName("table")
    
    For Each inputelement In Class1
    If inputelement.getAttribute("title") = "Submit" Then
    inputelement.Click
    Exit For
    End If
    Next
    Thank you very much for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width