I have a problem with the VBA code. I want to select and change the Option value
<div class="pull-left">
<span data-bind="text: allTasks.itemsFromIndex">1</span> To
<span data-bind="text: allTasks.itemsToIndex">10</span> Of
<span data-bind="text: allTasks.totalCount">39</span> All Pending Tasks
</div>
Show
<select data-bind="value: allTasks.pageSize">
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
Per Page
From your description, it looks like you want to select a dropdown option by automating the IE browser using VBA.
We can see that the dropdown element does not have an ID or Name to access it. so here we can try to find that specific element using its data-bind attribute value.
VBA code:
Sub demo()
Dim ie As Object
Dim selectElement As HTMLSelectElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://Your_Site_URL_here..." 'Modify the URL here...
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set selectElement = ie.document.querySelector("select[data-bind='value: allTasks.pageSize']")
selectElement.Options(3).Selected = True ' Note that index value starts from 0...
'ie.Quit
End Sub
Output:
Further, you can try to modify the code example as per your own requirements.
Related
First time trying this.
I have the following HTML in a web page and I am using Excel and VBA to scrape data based on user addresses in my excel file. The issue I am having is the combobox does not accept any input but it has to be a selection from its drop down list.
I can select the input field and add my entry, but the drop list does not appear. The site is using an autocomplete where the list is built by an AJAX call once it detects an input.
How do I trigger the drop list in the combobox and select the correct entry from it to match the input from my range (if it exists)? The code below is one of my various attempts.
The url is http://play.afl/club-finder/? and I am trying to enter a suburb name in the last field next to the GO button. Try, for example, Parramatta
An observation is that for the combobox element:
<input role="combobox" aria-autocomplete="list" value="Parramatta, NSW" autocomplete="off">
If I type the value manually into the field, inspect shows the value= changing as I type it. When the vba changes this value, it does not get changed in inspect.
Thank you
<div class="program-filters">
<span>Search for </span>
<a>
any gender <img src="/site/images/svg/DownArrow.svg" alt="Down Arrow" class="down-arrow-icon">
</a>
<span class="xs-hide">,</span>
<br class="sm-hide md-hide lg-hide">
<a>
all ages <img src="/site/image/svg/DownArrow.svg" alt="Down Arrow" class="down-arrow-icon">
</a>
to
<a>
play <img src="/site/images/svg/DownArrow.svg" alt="Down Arrow" class="down-arrow-icon">
</a>
<br class="sm-hide md-hide lg-hide"> near
<div style="display: inline-block;">
<input role="combobox" aria-autocomplete="list" autocomplete="off" value="SEARCH VALUE HERE">
</div>
<a class="in-map-btn btn btn-primary btn-go btn-mobile-fixed">GO</a>
</div>
My macro so far is:
Sub get_data()
Const myURL As String = "http://play.afl/club-finder/?"
Dim c As Range
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate myURL
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
For Each c In Range("MY_SEARCH")
Set searchBar = appIE.document.getElementsByClassName("program-filters")(0)
Set myInput = searchBar.getElementsByTagName("INPUT")(0)
myInput.Focus
myInput.Value = c.Value
myInput.FireEvent ("onchange")
Set myAutoComplete = searchBar.getElementsByClassName("autocomplete")(0)
If Not myAutoComplete Is Nothing Then
'FIND IN LIST AND SELECT HERE
searchBar.getElementsByClassName("in-map-btn btn btn-primary btn-go btn-mobile-fixed")(0).Click
Do While appIE.Busy
DoEvents
Loop
'COLLECT DATA HERE
End If
Next
appIE.Quit
Set appIE = Nothing
End Sub
Tried executing jQuery from vba to trigger the AJAX, with no errors but not working:
appIE.Document.parentWindow.execScript "jQuery('.program-filters input').trigger('change')"
Using the suggestion below still not working though no errors:
jq = "jQuery('.program-filters input').val('" & c.Value & "').trigger('change')"
appIE.Document.parentWindow.execScript jq
You probably both have to fill in a value in the input field (combobox) AND trigger the change event for this to work. Try this:
appIE.Document.parentWindow.execScript "jQuery('.program-filters input').val('yourvalue').trigger('change')"
I need to login in a website.
Every time I make the credential insterted with VBA, after the click event, It say that the password is not correct.
If I make the credential inserted by VBA and if I manually cancel one letter of the username and I rewrite it and if I manually cancel one letter of the password and I rewrite it, after I send the click event with VBA it works.
which is the reason? Shound I insert the credential using kinda keypress event? Which is the correct syntax for that?
thanks
please see below HTML code and what I wrote in VBA
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate "sss.html"
Do Until .ReadyState = 4
DoEvents
Loop
.Document.all.Item("login").Value = "username"
.Document.all.Item("password").Value = "password"
Set elems = ie.Document.getElementsByTagName("button")
For Each e In elems
If (e.innerHTML = "Confirm") Then
e.Click
Exit For
End If
Next e
<input type="text" ng-model="login" class="form-control ng-pristine ng-valid ng-touched" id="login" placeholder="Enter login (email)">
<input type="password" ng-model="password" class="form-control ng-pristine ng-valid ng-touched" id="password" placeholder="Password">
<button class="btn btn-default" ng-click="authenticate()" notranslate="">Confirm</button>
this website wants you to actually type your username/password and actually press the "Confirm" button, you can imitate it with SendKeys
so instead of setting the value and looping for the button element, use this code:
.Document.all.Item("login").focus
SendKeys "username", True
.Document.all.Item("password").focus
SendKeys "password", True
SendKeys "{TAB}", True
SendKeys "~", True
hope this helps.
Using a selenium wrapper, how can I input the data of my choosing into the input value placeholder below? This is for web scraping purposes. Site code below:
<input value="" placeholder="Enter Text" autocomplete="off" aria-
label="Enter Text: name and age required" data-
reactid=".9odxj7ein7k.0.0.9.0.0.$body.2.9.0.0.9.0.0.0.9.0">
Here's my current VBA code:
Private Sub CommandButton3_Click()
Dim bot As New WebDriver
bot.Start "chrome", "url”
Set the_input_elements = bot.Document.getElementsByID("input value")
For Each input_element In the_input_elements
If input_element.getAttribute("input value") = "" Then
input_element.Value = "Text I would like to enter to the form"
Exit For
End If
Next input_element
End Sub
I'm trying to find out how I can click on the following button as they are all named the same. As you can see the button is named "btn-default" twice, but I need to click on the one which contains Cloud.
The following code HTML which is a source of an internal website that I need to use for my VB application. Basically, this code is to show you what it looks like.
<div class="col-sm-4 col-md-4">
<a href="?login.type=enterprise">
<div class="btn-default">
Enterprise
</div>
</a>
</div>
<div class="col-sm-4 col-md-4">
<a href="?login.type=cloud">
<div class="btn-default">
Cloud
</div>
</a>
</div>
I would like to click on the cloud via VBA code.. I've got the following which doesn't do the trick.
The following code VBA which is the code I need for my excel VBA.
Set links = IE.document.getElementsByTagName("a")
For Each Hyperlink In links
If InStr(Hyperlink.href, "cloud") Then
Hyperlink.Click
Exit For
End If
Next
Problem is that I can't use the navigate option as it errors on this I really need to click on the button (btn-default) via code.
And the following solution also didn't do the trick
Dim ele As Object
For Each ele In IE.Document.getElementsByClassName("btn-default")
If InStr(ele.innerText, "Cloud") > 0 Then ele.Click
Next
It wasn't the code that was wrong just a wrong placement of my code, the following did work after adding a wait to IE:
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
For Each ele In IE.Document.getElementsByTagName("a")
If InStr(ele.innerText, "Cloud") > 0 Then
ele.click
Exit For
End If
Next
I am not fully sure I understood right as you're showing us HTML code but write that you want to use VBA.
Here is a VBA code if you want to create a button on excel that brings you to this link: "?login.type=enterprise"
Sub Browse()
Dim ie As Object
Set ie = CreateObject("Internetexplorer.Application")
ie.Visible = True
ie.Navigate "http://example.com/?login.type=enterprise"
End Sub
Then you can create a button by going to Developer\Insert\Button and assign it to the above macro by right clicking on it and chose "Assign Macro".
If that's not what you want, please be more precise in your question.
I'm attempting to edit this textbox using VBA:
<td id="ctl00_PlaceHolderMain_ctl00_PlaceHolderMain_ProfileEditorValueAboutMe" class="ms-authoringcontrols" valign="top" style="padding-left:10px;padding-right:10px;"><span class="ms-profilevalue"><span id="ctl00_PlaceHolderMain_ProfileEditorEditAboutMe"><DIV id="ctl00_PlaceHolderMain_ProfileEditorEditAboutMe_editableRegion" class="ms-rtestate-write ms-inputuserfield ms-long" contentEditable="true" InputFieldId="ProfileEditorEditAboutMe_hiddenRTEField" style="height:125px;overflow:scroll;background-color:white;"><p>text box content here</p></DIV></span>
<div class="ms-profiledescription" style="width: 386px; white-space:normal;">Please enter some details in the textbox</div>
</span>
<table border="0">
<tr>
<td></td>
</tr>
</table>
</td>
Here is the VBA, which performs as expected, other than that the textbox doesn't change (either on screen or when the form is saved by the click event):
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.Navigate "URL"
While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Debug.Print ie.Document.getelementbyid("ProfileEditorEditAboutMe_hiddenRTEField").Value
ie.Document.getelementbyid("ProfileEditorEditAboutMe_hiddenRTEField").Value = "<p>123</p>"
Debug.Print ie.Document.getelementbyid("ProfileEditorEditAboutMe_hiddenRTEField").Value
ie.Document.all("Button1").Click
The .value property does change, but the textbox does not.
I would be grateful for any ideas. As you can probably tell, I'm fairly new to VBA webscraping.
Since you're manipulating the value of the textbox yourself, you'll need to fire the onchange event in order for the textbox to repaint itself with your new value. This is done by creating an event object and then calling it via the dispatchEvent() function.
Dim e
Set e = ie.Document.getelementbyid("ProfileEditorEditAboutMe_hiddenRTEField")
' Set the value...
e.Value = "123"
' Fire the "onchange" event...
Dim objEvent
Set objEvent = ie.document.createEvent("HTMLEvents")
objEvent.initEvent "change", False, True
e.dispatchEvent objEvent