Selecting Dropdown list in IE with VBA - html

I have been trying to select a drop down list in a web page using VBA, which I'm new to. In HTML the drop down menu is stated as Button and not Select. Here is the HTML code:
<span class='btn-group'>
<button id='str_listing-btn' name='str_listing-btn' type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown' data-value='For Sale'>
For Sale
<span class='caret' style='margin-left:5px;'>
</span>
</button>
<ul class='dropdown-menu wptk_crud_dropdown' id='str_listing' data-value='str_listing' role='menu'>
<li>
<a href='#' data-value='For Sale'>For Sale</a>
</li>
<li>
<a href='#' data-value='For Rent'>For Rent</a>
</li>
<li>
<a href='#' data-value='Wanted To Buy'>Wanted To Buy</a>
</li>
<li>
<a href='#' data-value='Wanted To Rent'>Wanted To Rent</a>
</li>
</ul>
</span>
I have tried a few VBA codes to select one of the options. Below is the latest code that I have used but after running it nothing seems to happen to the drop down menu:
Private Sub InsertPropwall_Click()
Dim objIE1 As Object
objIE1.navigate ("http://www.propwall.my/classifieds/post_ad?action=add")
Do
DoEvents
Loop Until objIE1.ReadyState = 4
objIE1.Document.getElementById("str_listing-btn").Value = "For Rent"
Do
DoEvents
Loop Until objIE1.ReadyState = 4
End Sub

The dropdown isn't a dropdown (as you've mentioned). Instead, it's an unordered list with a number of links as clickable options. With that said, the For Rent option isn't contained in str_listing-btn. Notice how that tag closes before any of the options are available. Instead, it's contained in the list below it.
Since I couldn't get into your link without creating an account, I tested out code to select the For Rent option on the main page. Take a look at my code, test it to see if it does what you need, then try to accommodate it into your code. Let us know if you need additional help.
Sub NavigateIt()
Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.Application")
oIE.navigate ("http://www.propwall.my/classifieds")
oIE.Visible = True
Do
DoEvents
Loop Until oIE.ReadyState = 4
Set AvailableLinks = oIE.document.getelementbyid("list-listing").getelementsbytagname("a")
For Each cLink In AvailableLinks
If cLink.innerhtml = "For Rent" Then
cLink.Click
End If
Next cLink
End Sub

CSS selector:
You could have used a CSS selector e.g.
a[data-value=For Sale] or a[data-value=For Rent]
This says first element with a tag that has attribute data-value whose value is 'For Sale'; Or, 'For Rent' in the subsequent example.
CSS query:
VBA:
You apply the CSS selector using the querySelector method of document
objIE1.Document.querySelector("a[data-value=For Sale]").Click

Related

Not sure what element I need to click in IE HTML for VBA?

So I have the following HTML code (I'm not super familiar with HTML so excuse the incorrect spacing):
<div class="section_heading">
<span class="section_anchor" id="ajax_result_table_link" data-label="vs. Pitchers"></span>
<h2>vs. Pitchers</h2>
<div class="section_heading_text">
<ul>
<i>Sorting only sorts the values in the table shown</li>
<li>Click a player to see each PA</li>
<li class="hasmore drophover">
<span href="javascript:void(0)">Share & more</span>
<div>
<div class="closer" role="button" aria-label="close this menu"></div>
<ul>
<li><button class="tooltip" id="share_on_ajax_result_table" tip="Use a customizable report creator that can<br>output HTML, CSV, or a shareable link.">Modify & Share Table</button>
</li>
<li><button class="tooltip" tip="Get a bit of widget code to emed this table on your site">Embed this Table</button></li>
<li><button class="tooltip" tip="Convert the table below to comma-separated values<br>suitable for use with excel">Get as Excel Workbook (experimental)</button><a id="dlink" style="display: none;"></a></li>
<li><button class="tooltip" tip="Export table as <br>suitable for use with excel">Get table as CSV (for Excel)</button></li><li><button class="tooltip" tip="">Strip Mobile Formatting</button></li>
From this URL: https://www.baseball-reference.com/play-index/batter_vs_pitcher.cgi?batter=mancitr01
I am trying to use VBA to download the Excel workbook (under the "Share & More" Dropdown), but I can't seem to be able to click the link. I've tried using get element by class and get element by ID but they don't seem to work. I'm guessing this HTML code is a little more advanced than I'm used to. I've been trying to click the ID "dlink" but I'm not sure if that is the end goal.
Any help or input would be appreciated.
EDIT:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.baseball-reference.com/play-index/batter_vs_pitcher.cgi?batter=mancitr01"
Wait 10
IE.document.getElementById("dlink").Click
Exit Sub
Set oElementCollection = IE.document.getElementsByClassName("section_heading_text")
For Each span In oElementCollection
If span.innerHTML = "Share & more" Then
span.ParentNode.Click
Exit For
End If
Next span
Exit Sub

How to bypass <li> clear tag

I am using below mentioned code for data extraction,but due to li clear tag unable to copy complete data using access vba.Guidence required regarding bypassing clear tag.My code is given below.
Set my_data = html4.getElementsByClassName("right_box")
For Each Item In my_data
Set my_data1 = Item.getElementsByTagName("li")
For Each item1 In my_data1
If item1.innerHTML Like "*href*" Then
href11 = item1.getElementsByTagName("a")
Else
Exit For
End If
And HTML data code is given below.
<div class="right_box"> <div class="right_box_title"> <div class="title_left"></div> <a class="title_right" href="products.php?disp=1"></a> </div> <ul class="pro_list">
<li> <a title="NEW Handbags Handbags7" href="/index.php/NEW-Handbags-Handbags72-p20253745.html" class="pic"><img title="NEW Handbags Handbags7" alt="NEW Handbags Handbags7" src="/image.php?pic=2017-08-27%2F2017082722393889955047.jpg&style=1&folder=uploadImage%2F" border="0" /></a>
</li>
<li class="clear"></li>
<li>
<a title="NEW Handbags Handbags6" href="/index.php/NEW-Handbags-Handbags6-p2025361.html" class="pic"><img title="NEW Handbags Handbags6" alt="NEW Handbags Handbags6" src="/image.php?pic=2017-08-27%2F201708272239272285106.jpg&style=1&folder=uploadImage%2F" border="0" /></a>
</li>
Above code stopped data at clear
A CSS class should not prevent you from gathering data.
Note: You should set a reference to the Microsoft HTML Object Library.
This line of ↓ Code ↓ should fail due to item1.getElementsByTagName("a") returning an object and not a scalar value.
href11 = item1.getElementsByTagName("a")
Here is a better pattern for iterating over anchor tags:
Dim a As HTMLAnchorElement
Set my_data = html4.getElementsByClassName("right_box")(0)
For Each a In my_data.getElementsByTagName("a")
Debug.Print a.href
Next
remove this li with clear class and manage the design using css appied in other li.
In this case you could use querySelectorAll and pass appropriate selector like this one div[class='right_box'] ul[class='pro_list'] li a which select all a inside li inside ul with class pro_list inside div with class right_box. For more information about selectors see e.g. this page. HTH
Set html4 = ie.document
Dim selector As String
selector = "div[class='right_box'] ul[class='pro_list'] li a"
Dim anchors As IHTMLDOMChildrenCollection
Set anchors = html4.querySelectorAll(selector)
Dim anchor, i
If Not anchors Is Nothing Then
For i = 0 To anchors.Length - 1
Set anchor = anchors.Item(i)
Debug.Print "anchor-" & i & " href: " & anchor.href
Next i
End If
Output:
anchor-0 href: file:///C:/index.php/NEW-Handbags-Handbags72-p20253745.html
anchor-1 href: file:///C:/index.php/NEW-Handbags-Handbags6-p2025361.html

VBA does not click <a> within <li>

I am navigating to a webpage with an unordered list. Now I have to click an 'anchor' tag within a specific 'li' tag.
The part of the source code is,
<UL class="x-tab-strip x-tab-strip-top" id=ext-gen151>
<LI id=infoPageinfoPanelID__infoPage_myTab_pubst_pubstructStructureGWT _nodup="30817">
<A class=x-tab-strip-close></A>
<A class=x-tab-right href="#">
<EM class=x-tab-left>
<SPAN class=x-tab-strip-inner>
<SPAN class="x-tab-strip-text ">Structure</SPAN>
</SPAN>
</EM>
</A>
</LI>
</UL>
The anchor tag does not have a name or ID and has a class name(" x-tab-right ").
I tried the following vba code for simulating a click on that tag,
Dim targetSpan As HTMLObjectElement
Set targetSpan = doc.getElementById("infoPageinfoPanelID__infoPage_myTab_pubst_pubstructStructureGWT").getElementsByTagName("a")(1)
targetSpan.click
=> Code :
Dim AllSpanElements As IHTMLElementCollection
Dim spanCounter As Long
Set AllSpanElements = doc.getElementsByTagName("li")
For spanCounter = 0 To AllSpanElements.Length - 1
With AllSpanElements(spanCounter)
If (.innerText) = "Structure" Then
.ParentElement. ParentElement.ParentElement.Click
Exit For
End If
End With
Next
I got the 2nd code from StackOverflow.
Both the code doesn't do anything. What am I doing wrong?
Thanks in advance.

Doubts in clicking Unordered List in IE using VBA

I have an unordered list with links in IE and the list is also dynamic. Clicking on the 1st level in the list will open the 2nd level and so on. My work is to select one link within 4th level. So I have to click upto 4th level and click another option after that.
Initially, without clicking anything, the html code of the list will be,
<div class="tree" id="treeDiv" style="width: 288px; border-top-color: transparent; border-top-width: 195px; border-top-style: solid;" ondragstart="return false" onselectstart="return false" oncontextmenu="return false">
<ul style="width: 915px;">
<li>
<a class=" open" id="level1st" onclick="resetTree(0);" href="#">GENERAL </a>
<ul></ul>
</li>
</ul>
</div>
After clicking "GENERAL" (1st level), the html code of the list will be,
<div class="tree" id="treeDiv" style="width: 288px; border-top-color: transparent; border-top-width: 195px; border-top-style: solid;" ondragstart="return false" onselectstart="return false" oncontextmenu="return false">
<ul style="width: 915px;">
<li>
<a class=" open" id="level1st" onclick="resetTree(0);" href="#">GENERAL </a>
<ul>
<li>
<a class=" open" id="0/2" style="padding-left: 13px;" href="#">GENERAL </a>
<ul></ul>
</li>
</ul>
</ul>
</div>
To click 1st level, I tried writing two kinds of codes,
CODE 1-----
Sub A()
Dim ie As InternetExplorerMedium
Dim availableLinks As MSHTML.HTMLElementCollection
Dim cLinks As MSHTML.HTMLLIElement
Set ie = New InternetExplorerMedium
ie.Navigate 'URL
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set availableLinks = ie.document.getElementsByClassName("tree").getElementsByTagName("li")
For Each cLinks in availableLinks
If cLinks.innerText = "General" Then
cLinks.click
End If
Next cLinks
End Sub
CODE 2-----
Sub B()
Dim ie As InternetExplorerMedium
Dim availableLinks As MSHTML.HTMLElementCollection
Dim cLinks As MSHTML.HTMLLIElement
Set ie = New InternetExplorerMedium
ie.Navigate 'URL
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set availableLinks = ie.document.getElementsByClassName("tree").getElementsByTagName("a")
For Each cLinks in availableLinks
If cLinks.ID = "level1st" Then
cLinks.Focus
cLinks.FireEvent("onclick")
End If
Next cLinks
End Sub
Now about my doubts,
Assuming my CODE 1 is correct, I am able to select my 1st level thro' innerText 'General'. But my 2nd level also has the same innerText 'General'. So how do I specify in code that it is the 2nd level.
Also, in CODE 1, I have used cLinks.click. Is it correct or should I use cLinks.FireEvent("onclick")?
Now in CODE 2, I have declared cLinks As MSHTML.HTMLLIElement. But I am not using any <li> tag in the code. So what should I declare it as, as MSHTML.HTMLLIElement or as MSHTML.HTMLElement?
Finally, is CODE 1 and CODE 2 correct pertaining to my unordered list? I cannot execute my code and check now as I have to go to my office to do that.
So what do you think? Which one is suitable? I think CODE 2 would be appropriate for all the levels.
If both the codes are wrong, can you suggest a suitable one?
Sorry for the very long post, I am just learning web-scraping so I am full of doubts for which I am not able to find solutions in internet.
Can anyone please help? Thanks in advance.

Can i click on a button by de href or the class?

I'm current trying to click on a button but it has no id or class name, just class and href.
The html code is:
<a href="#" class="bt-acessar-login radius" onclick="validarFornecedor()" ;="">
<i></i>
<span>Acessar sistema</span>
<div class="clear"></div>
</a>
And the vba code I tried is:
Set btnObject = .document.getElementByClass("bt-acessar-login radius")
btnObject.FireEvent ("onclick")
i search some more and find this code who works for me !
Set elems = .document.getElementsByClassName("bt-acessar-login radius")
For Each e In elems
If (e.getAttribute("onclick") = "validarFornecedor()") Then
e.Focus
e.Click
Exit For
End If
Next e