Namita
Namita

Reputation: 21

Select Radio Button / input value in Open box on website using vba

I am trying to punch the radio button question and want to input the value in an open text box as well (using VBA). Below is the script I am using.

I tried multiple things, looked at other websites as well but it is not working.

It would be great if someone can guide me.

Dim IE As Object
Dim Region, VOCSentDate As String

Sheets("Sheet1").Select
Region = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
VOCSentDate = ThisWorkbook.Worksheets("Sheet1").Range("B1").Value

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "website link....."
Application.StatusBar = "Submitting"
While IE.Busy
DoEvents
Wend

I'm not sure how to punch the radio button question. I tried the 3 types for Open textbox below but none of them are not working, throwing object required error

IE.Document.getElementById("ctl00_body_7F22FA6E-5D77-426A-AA84-68D833FA05C1_3").Value = VOCSentDate

IE.Document.getElementById("ctl00_body_7F22FA6E-5D77-426A-AA84-68D833FA05C1_3").innerText = VOCSentDate

IE.Document.all("ctl00_body_7F22FA6E-5D77-426A-AA84-68D833FA05C1_3").Value = VOCSentDate

Application.StatusBar = "Form Submitted"
IE.Quit
Set IE = Nothing

-------------- HTML of the Radio button --------------

<div class="SurveyItem  RadioButtonList Item1">
  <div class="ItemText">
    <span class="NoItemNumber"></span><span class="QuestionText">Please select your region:</span>
  </div>
  <div class="Validators">
    <span id="ctl00_body_ctl12" style="color:Red;display:none;">A response to this question is required</span>
  </div>
  <div id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1" class="Response">
    <div class="ResponseOption">
      <input id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_1" type="radio" name="ctl00$body$ctl00_body_ctl00_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1" value="01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_1" /><label for="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_1">Americas</label>
    </div>
    <div class="ResponseOption AlternatingRow">
      <input id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_2" type="radio" name="ctl00$body$ctl00_body_ctl00_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1" value="01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_2" /><label for="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_2">ANZ</label>
    </div>
    <div class="ResponseOption">
      <input id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_3" type="radio" name="ctl00$body$ctl00_body_ctl00_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1" value="01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_3" /><label for="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_3">APAC</label>
    </div>
    <div class="ResponseOption AlternatingRow">
      <input id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_4" type="radio" name="ctl00$body$ctl00_body_ctl00_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1" value="01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_4" /><label for="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_4">APME</label>
    </div>
    <div class="ResponseOption">
      <input id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_5" type="radio" name="ctl00$body$ctl00_body_ctl00_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1" value="01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_5" /><label for="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_5">Europe</label>
    </div>
    <div class="ResponseOption AlternatingRow">
      <input id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_6" type="radio" name="ctl00$body$ctl00_body_ctl00_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1" value="01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_6" /><label for="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_6">India</label>
    </div>
    <div class="ResponseOption">
      <input id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_7" type="radio" name="ctl00$body$ctl00_body_ctl00_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1" value="01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_7" /><label for="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_7">North America</label>
    </div>

------------------- HTML of Open End -------------------------

<div class="SurveyItem  Item3">
  <div class="ItemText">
    <span class="NoItemNumber"></span><span class="QuestionText">EOPR First Sent Date (dd/mm/yyyy)</span>
  </div>
  <div class="Validators">
    <span id="ctl00_body_ctl52" style="color:Red;display:none;">A response to this question is required</span><span id="ctl00_body_ctl53" style="color:Red;display:none;">Incorrect Date Format</span>
  </div>
  <div class="Response">
    <input name="ctl00$body$01EE9560-B1F9-4BA3-A922-9D53A1120FC2_3" type="text" id="ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_3" class="TextBox" />
  </div>
</div>

Upvotes: 2

Views: 297

Answers (2)

QHarr
QHarr

Reputation: 84465

tl;dr;

The following are based on the HTML as shown. If there are parent form/frame/iframe tags involved they will also need to be negotiated before making the selections below.


Radio buttons:

For the radio buttons you can use a CSS selector combination to target the page styling and return a nodeList of all of the radio button elements. You can then select by index the appropriate button

Dim aNodeList As Object
Set aNodeList = ie.document.querySelectorAll(".ResponseOption [type=radio]")
aNodeList.item(0).Click '<==Select first  option

I think click may be likely method here as I can't see a checked attribute, else the syntax would be aNodeList.item(0).checked = True

The returned nodeList is as follows by index


Textbox:

There is an ID for the input box so you can use an ID selector, #, to target

ie.document.querySelector("#ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_3").Value = "myText"

Upvotes: 1

user4039065
user4039065

Reputation:

To choose a radio button style input, make the checked property true.

IE.Document.getElementById("ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_1_6").checked = true

An input textbox gets its value set.

IE.Document.getElementById("ctl00_body_01EE9560-B1F9-4BA3-A922-9D53A1120FC2_3").value = format(VOCSentDate, "d/m/yy")

Upvotes: 0

Related Questions