Choosing a value from Aria Control Dropdown using VBA - excel

I'm trying to add a click and select value from dropwdown for this HTML code.
I'm unable to make the click change.
I tried getelementbyID and getelementbyclassname.
<form method="POST" action="/submit" accept-charset="UTF-8" id="project-form">
<div class="form-fields">
<h1>Basics</h1>
<div class="control-groups">
<label for="project_requestor">Primary Delivery Contact / Project Requestor
<span>*</span></label>
<select class="required tooltip dropdown" data-placeholder="Select Sub- Category"
id="project_requestor" name="project_requestor" data-type="contact_name">
<option value="0">Select Requestor</option>
</select>
</div>

Assuming you are using IE and that you have a proper wait before attempting these i.e.
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
To click dropdown:
ie.document.querySelector("#project_requestor").click
To select first option:
ie.document.querySelector("#project_requestor [value='0']").Selected = True
The # is a css id selector. The [] is a css attribute selector. These target the elements by the css of the page.

Related

How to click on the button after data paste

I am trying to click on the button after I have pasted a value on a text box. However none of the code I tried seems to work. On the same column there are 2 text boxes and few buttons. I managed to open up a new frame after clicked on the 1st button with the below code:
Set the_button_elements = doc.getElementsByTagName("div")
For Each button_element In the_button_elements
If button_element.getAttribute("class") = "pzbtn-mid" Then
button_element.Click
Exit For
End If
Next button_element
I have tried by changing the tag name and attribution and also the below code but it still doesn't work:
doc.querySelectorAll("[type=button]").item(3).Click
Below is the element for the 1st button which work with the above code:
<button type="button" class="pzhc" id="AcctNumber"
disabled="" onclick="this.disabled=true;javascript: LoadAcct();"
title="Search">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
<div class="pzbtn-mid" data-click="...">
<img class="pzbtn-i">Go</div></div></div></button>
Below is the element for the next button which I am trying to find the code to make it work:
<button type="button" class="pzhc" id="SearchBtn"
disabled="true"onclick="getCaseDetails();">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">`enter code here`
<div class="pzbtn-mid">
<img class="pzbtn-i">Search</div></div></div></button>
Appreciate someone could guide me by providing me the code that click on the button as the button is still grey out even after the text pasted. However this is the same situation for the 1st code but it still works.
Perhaps the issue relates to the disabled attribute. If we want to click the button, we should make sure it is enabled status. After clicking the first button or in the textbox change event, you could use the following JavaScript code to enable the Search button:
document.getElementById("SearchBtn").disabled = false;
You could check the following sample:
<button type="button" class="pzhc" id="AcctNumber" onclick='this.disabled=true;javascript: alert("Go"); document.getElementById("SearchBtn").disabled = false;'
title="Search">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
<div class="pzbtn-mid" data-click="...">
<img class="pzbtn-i">Go
</div>
</div>
</div>
</button>
<button type="button" class="pzhc" id="SearchBtn"
disabled="true" onclick='javascript: alert("Search");'>
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
`enter code here`
<div class="pzbtn-mid">
<img class="pzbtn-i">Search
</div>
</div>
</div>
</button>
Then, if I use the following VBA script click the button, it will click the Go button, then, enable the search button and click it:
Sub login()
Const Url$ = "https://dillion132.github.io/vbatestpage.html"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate Url
ieBusy ie
.Visible = True
'Find the related button
Dim button_go_element As Object, button_search As Object
Set button_go_element = .document.getElementById("AcctNumber")
Set button_search = .document.getElementById("SearchBtn")
button_go_element.Click
button_search.Click
End With
End Sub
Sub ieBusy(ie As Object)
Do While ie.Busy Or ie.readyState < 4
DoEvents
Loop
End Sub

Unable to select a value from dropdown in webpage using selenium VBA

I am trying to select a value from a dropdown in a web page using Seleinum with VBA binding, but I am unable to do it.
This is the code I tried bot.FindElementById("holder-samples-date-type-inputEl").SendKeys "XX"
I am not getting any error the code runs it click the dropdown button and the list is shown but no value is selected and the dropdown does not change.
I have the same project with VBA and IE (DOM elements) and this code works fine in Internet explorer IE.document.getElementById("holder-samples-date-type-inputEl").Value = "XX"
The interesting part of this dropdown is after clicking the dropdown icon i am unable to inspect the list (like do a right click on the list) and if i click anywhere else in the web page or change the window the dropdown list get closed (like it retracts) note: this is not a mouseover drop down. Also, when the list is open there seems to be additional code added (I have pasted a snippet of the code)
Below is the HTML from the web page.
<div id="holder-samples-date-type-triggerWrap" data-ref="triggerWrap" class="x-form-trigger-wrap x-form-trigger-wrap-default x-form-trigger-wrap-invalid">
<div id="holder-samples-date-type-inputWrap" data-ref="inputWrap" class="x-form-text-wrap x-form-text-wrap-default x-form-text-wrap-invalid"><input id="holder-samples-date-type-inputEl" data-ref="inputEl" type="text" role="combobox" aria-label="Chargebacks: Date Type" aria-readonly="false" aria-required="true" aria-haspopup="true" aria-expanded="false" aria-autocomplete="list" size="1" name="P_samples_Date_Type" readonly="readonly" class="x-form-field x-form-required-field x-form-text x-form-text-default x-form-invalid-field x-form-invalid-field-default" autocomplete="off" aria-labelledby="holder-samples-date-type-labelEl" aria-owns="boundlist-1206" componentid="holder-samples-date-type" data-errorqtip="<ul class="x-list-plain"><div>Date Type</div><li>This field is required</li></ul>" aria-invalid="true"></div>
<div id="holder-samples-date-type-trigger-picker"
aria-label="Trigger" class="x-form-trigger x-form-trigger-default x-form-arrow-trigger x-form-arrow-trigger-default "><img class="combo-trigger-placeholder" src="https://www.sample.net/clr/images/16x16_transparent.png" title="combobox trigger" alt="combobox trigger"></div>
</div>
Below is the image
Since I am unable to do a right click on the dropdown option. I did a 'Find" option in the developer window and below is the webpage code for the dropdown item and it is inside a iframe
<div id="boundlist-1206-listWrap" data-ref="listWrap" role="presentation" class="x-boundlist-list-ct x-unselectable" style="overflow: auto; height: auto;">
<ul id="boundlist-1206-listEl" data-ref="listEl" class="x-list-plain">
<div data-qtip="" class="x-boundlist-item x-boundlist-item-over" tabindex="-1" data-recordindex="0" data-recordid="31" data-boundview="boundlist-1206" id="ext-932" role="option" aria-selected="false">Received Date</div>
<div data-qtip="" class="x-boundlist-item" tabindex="-1" data-recordindex="1" data-recordid="32" data-boundview="boundlist-1206" id="ext-933" role="option" aria-selected="false">Status Date</div>
<div data-qtip="" class="x-boundlist-item" tabindex="-1" data-recordindex="2" data-recordid="33" data-boundview="boundlist-1206" id="ext-934" role="option" aria-selected="false">Due Date</div>
<div data-qtip="" class="x-boundlist-item" tabindex="-1" data-recordindex="3" data-recordid="34" data-boundview="boundlist-1206" id="ext-935" role="option" aria-selected="false">Disposition Date</div>
</ul>
</div>
Some points that may help:
1) If inside an iframe you will need to switch to the iframe first
bot.SwitchToFrame(identifier)
Identifier can be the element selected by iframe name/id etc.
2) You can try click and hold on the input text box before using your SendKeys
bot.FindElementById("x").ClickAndHold
'sendkeys line here
3) You can try setting the value with javascript
bot.ExecuteScript "document.getElementById('x').value = 'xx';"

VBA fire event on empty HTML div

I try to automate some manual processes by using VBA automation. One of them is to click on a element that has an empty content and at the moment I am not able to figure out how to deal with it
The HTML code that I am trying to click on:
<div id="searchcombobox-1077-triggerWrap" data-ref="triggerWrap" class="x-form-trigger-wrap x-form-trigger-wrap-toolbar">
<div id="searchcombobox-1077-inputWrap" data-ref="inputWrap" class="x-form-text-wrap x-form-text-wrap-toolbar">
<input id="searchcombobox-1077-inputEl" data-ref="inputEl" type="text" role="combobox" size="1" name="searchcombobox-1077-inputEl" placeholder="Account Number" tabindex="-1" class="x-form-field x-form-text x-form-text-toolbar " autocomplete="off" componentid="searchcombobox-1077">
</div>
<div id="searchcombobox-1077-trigger-picker" class="x-form-trigger x-form-trigger-toolbar x-form-search-trigger x-form-search-trigger-toolbar "></div>
</div>
The VBA code used is:
HTMLDoc.getElementById("searchcombobox-1077-inputEl").Value = '11xx111'
Set click_el = HTMLDoc.querySelector("#searchcombobox-1077-trigger-picker")
With click_el
.Focus
.FireEvent "onclick"
End With
What should be the approach that I need to take into consideration since the div element that I need to click on is empty?
Thanks,

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

Select a dropdown using Python + Selenium

I am writing an automation for work and am stuck with a dropdown. The particular select box in question is as follows:
<span class="a-dropdown-container" id="select-size-bulk-asin">
<select name="display_type" class="a-native-dropdown">
<option value="SMALL-IMAGES">SMALL-IMAGES</option>
<option value="LARGE-IMAGES">LARGE-IMAGES</option>
<option value="TEXT">TEXT</option>
</select>
<span tabindex="-1" data-a-class="a-spacing-small" class="a-button a-button-dropdown a-spacing-small">
<span class="a-button-inner">
<span class="a-button-text a-declarative" data-action="a-dropdown-button" aria-haspopup="true" role="button" tabindex="0" aria-pressed="false" aria-owns="2_dropdown_combobox">
<span class="a-dropdown-prompt">SMALL-IMAGES</span>
</span>
<i class="a-icon a-icon-dropdown"></i>
</span>
</span>
</span>
It defaults to 'SMALL Images' and I would like to select the 'TEXT' option. I am receiving element not clickable error. The page is simple and the element is visible on the screen.
The list of methods I did try are:
Used WebDriverWait to wait for the element to be visible;
Used WebDriverWait to wait for the element to be clickable;
Used the select class to set the selected option;
I also read through a question.
I am thinking if I should just go to the next element and send Shift+Tabs until I reach this drop down and then down arrow keys. But would like to use that only as the last resort.
NOTE:
- I am using Python 3 and Chrome.
You can try this code to select value from drop down :
select = Select(driver.find_element_by_id('select-size-bulk-asin'))
select.select_by_visible_text('TEXT')
However,as you have mentioned you are receiving element not clickable exception. you can try this code :
WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.ID, "select-size-bulk-asin")))
As a last resort you can go ahead with :
drop_down= driver.find_element_by_id("select-size-bulk-asin")
drop_down.click()
actions = ActionChains(driver)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ENTER)
actions.perform()

Resources