Octavien
Octavien

Reputation: 1

Unable to select Radiobutton in website using VBA

Here is the code I am using:

Sub Websitedata()
    Dim ie As Object
    Dim myURL As String
    myURL = "FIRST PAGE"

    Set ie = New InternetExplorerMedium
    ie.Visible = True
    ie.navigate myURL

    Do While ie.readyState <> 4 And ie.Busy
        DoEvents
    Loop

    'Selects a type from a dropdown bar
    ie.document.forms("actionForm").elements("dropdownbar").Value = "311"
    'Inputs data
    ie.document.forms("actionForm").elements("inputbox1").Value = ""
    'Inputs data
    ie.document.forms("actionForm").elements("inputbox2").Value = ""
    'Inputs data
    ie.document.forms("actionForm").elements("inputbox3").Value = ""
    'Inputs data
    ie.document.forms("actionForm").elements("inputbox4").Value = ""
    'Inputs data
    ie.document.forms("actionForm").elements("inputbox5").Value = ""
    'Pressed submit and goes to next page.
    ie.document.forms("actionForm").elements("_eventId_Search").Click
    'Needs to press a radiobutton.
    ie.document.getElementByName("declarationId").Click
End Sub

I am having issue clicking the radio button. Tried checked, click, value everything.

Here is the HTML of the page:

<TABLE id=declarationList class="list sortable" cellSpacing=0 cellPadding=0 width="100%"><TBODY>
<TR class=header>
    <TH style="TEXT-ALIGN: center" align=center></TH>
    <TH><A onclick="ts_resortTable(this);return false;" class=sortheader href="">Type<SPAN class=sortarrow sortdir="down">&nbsp;&nbsp;?</SPAN></A></TH>
    <TH><A onclick="ts_resortTable(this);return false;" class=sortheader href="">rts<SPAN class=sortarrow>&nbsp;&nbsp;&nbsp;</SPAN></A></TH>
    <TH><A onclick="ts_resortTable(this);return false;" class=sortheader href="">tyi<SPAN class=sortarrow>&nbsp;&nbsp;&nbsp;</SPAN></A></TH>
    <TH><A onclick="ts_resortTable(this);return false;" class=sortheader href="">trtjm<SPAN class=sortarrow>&nbsp;&nbsp;&nbsp;</SPAN></A></TH>
    <TH><A onclick="ts_resortTable(this);return false;" class=sortheader href="">erch<SPAN class=sortarrow>&nbsp;&nbsp;&nbsp;</SPAN></A></TH>
    <TH><A onclick="ts_resortTable(this);return false;" class=sortheader href="">dft<SPAN class=sortarrow>&nbsp;&nbsp;&nbsp;</SPAN></A></TH>
</TR>
<TR class=evenRow>
    <TD style="TEXT-ALIGN: center" align=center><INPUT onclick="updateButtons('Import','GF','79295876',false,'null',false,true,true,false,false,false)" type=radio value=79295876 name=declarationId> </TD>
    <TD>IM A </TD>
    <TD>4561548 </TD>
    <TD>29/03/2018 </TD>
    <TD>32856 </TD>
    <TD>0313 </TD>
    <TD>40 00 , 40 00 </TD>
</TR>
</TBODY></TABLE>

I need it to select the radio button which appears in the next page. Do I have to change some kind of data in the top? Please let me know. I am confused.

Update- Still no answer for this query. Please help

Upvotes: 0

Views: 159

Answers (1)

QHarr
QHarr

Reputation: 84465

You could try using a CSS selector

ie.document.querySelector("input[type=""radio""]").Click

CSS selector tested using HTML supplied:

CSS query

More about CSS selectors here: CSS Selectors


Notes:

If the page is being refreshed, due to a prior click, then you may need, after that click, to introduce a wait:

Application.Wait Now + TimeSerial(0,0,3)
ie.document.querySelector("input[type=""radio""]").Click    

or,

While .Busy = True Or .readyState < 4: DoEvents: Wend  
ie.document.querySelector("input[type=""radio""]").Click    

A combination of the above or even a loop, with a timeout, attempting to set the element for clicking:

Dim a As Object, exitTime As Date
exitTime = Now + TimeSerial(0, 0, 5)

Do
    DoEvents
    On Error Resume Next
    Set a = IE.document.querySelector("input[type=""radio""]")
    On Error GoTo 0
    If Now > exitTime Then Exit Do
Loop While a Is Nothing

a.Click

If a new window if opened then you will need to look for that window's handle potentially, or the window with the highest count (most recent). There are methodss on SO detailing this.

Here is an example of looping input tags looking for one with a type attribute of radio. This depends on whether you have more than one as to whether it clicks the right one, but gives you an idea.

Option Explicit
Public Sub test()
    Dim n As HTMLDocument
    Set n = New HTMLDocument
    n.body.innerHTML = [A1]  '<== your sample of HTML place in a cell

    Dim aList As Object, item As Object
    Set aList = n.getElementsByTagName("input")
    For Each item In aList
        On Error Resume Next
        If InStr(item.getAttribute("type").innerText, "radio", 1) > 0 Then
            Debug.Print item.outerHTML
            item.Click
            Exit For
        End If
        On Error GoTo 0
    Next item
End Sub

Upvotes: 1

Related Questions