Clicking an item on a drop-down menu with VBA - excel

I am having trouble clicking an item on a drop-down menu with VBA. I am able to click the drop-down button to show the options, but I cant click on a specific item. Below is the item I want to click on.
<a class="ActiveLink" title="Excel" alt="Excel"
onclick="$find('ctl31').exportReport('EXCELOPENXML');"
href="javascript:void(0)" style="padding: 3px 8px 3px 32px; display: block;
white-space: nowrap; text-decoration: none;">Excel</a>
There are 5 possible selections in the dropdown. "title" and "alt" are unique to this selection. The "class" is unique to all items of the dropdown. I tried the following.
IE.Document.getElementsByClassName("ActiveLink")(4).Click
I am aware that the 4 indicates the 5th element.
The error is
Object doesn't support this property or method.
I have resolved with the following code.
Set HTMLdoc = IE.Document
For Each l In HTMLdoc.getElementsByTagName("a")
If l.Title = "Excel" Then
l.Click
Exit For
End If
Next
End Sub

Related

In jsf, how to wrap the text in select option(SelectOneMenu)

In SelectOneMenu, one of the value is overflowing in drop-down and that value should be aligned according with other value.
Following skinning styles are available for selectOneMenu
.ui-selectonemenu Main container.
.ui-selectonemenu-label Label of the component.
.ui-selectonemenu-trigger Container of dropdown icon.
.ui-selectonemenu-items Items list.
.ui-selectonemenu-items Each item in the list.
try the following for your menu
.ui-selectonemenu-items {
display: inline !important;
}
or you can use
word-wrap: break-word;

How to add a tick to a checkbox through excel vba ie automation?

The checkbox I am trying to add a tick to is part of an online table and doesn't seem to be coded as a checkbox.
I have tried the following to add a tick and none work:
IE.Document.getElementByID("gridcolumn-1658-titleEl").Click
IE.Document.getElementByID("gridcolumn-1658-textEl").Click
IE.Document.getElementsByClassName("x-column-header-inner x-column-header-over")(0).Click
IE.Document.getElementsByClassName("x-column-header-inner")(0).Click
When I inspect the element, I get to the following, but none of this looks anything like a checkbox (the third one is the actual reference):
<div class="x-column-header x-column-header-checkbox x-column-header-align-left x-box-item x-column-header-default x-unselectable x-column-header-first" style="border-width: 1px; width: 24px; right: auto; left: 0px; top: 0px; margin: 0px; height: 24px;" id="gridcolumn-1658"><div id="gridcolumn-1658-titleEl" class="x-column-header-inner" style="padding-top: 6px; padding-bottom: 6px;"><span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span></div></div>
<div id="gridcolumn-1658-titleEl" class="x-column-header-inner" style="padding-top: 6px; padding-bottom: 6px;"><span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span></div>
<span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span>
The website is OptimoRoute, which can be accessed fairly quickly using a new log in, for those interested!
The button I am trying to click is the top one in the table.
Please refer to the following sample code:
Sub main()
'we define the essential variables
Dim IE As Object, Data As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("https://dillion132.github.io/vbacheckbox.html")
While IE.ReadyState <> 4
DoEvents
Wend
Set Data = IE.Document.getElementsByClassName("check")
Debug.Print Data.Length
If Len(Data) > 0 Then
For Each ee In Data
'Debug.Print ee.Value
'Based on the checkbox value to check/uncheck the checkbox.
If ee.Value = "Cat" Then
ee.Checked = True
End If
'check whether the checkbox is checked, then, get the checked value.
If ee.Checked Then
Debug.Print ee.Value & " is checked"
End If
Next ee
End If
End With
Set IE = Nothing
End Sub
Code in the website:
<input class="check" type="checkbox" value="Cat"> Cat </input>
<br />
<input class="check" type="checkbox" value="Dog" >Dog</input>
<br />
<input class="check" type="checkbox" value="Pig" checked ="checked" >Pig</input>
The result as below:

How to select and click dropdown menu item with selenium

I'm looking to sort a webpage from low price to high price by using the built-in product sort
I am able to successfully select the correct dropdown menu using xpath as follows:
WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH,
'//*[#id="react-views-container"]/div/div/div/div[1]/div/div/div/div[2]/div[1]/div/div[2]/div/div[1]/button')))
This was intended only to be a test of sorts, as I am still relatively new to selenium. However, given this worked I cannot understand why the following does not:
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,
'//*[#id="react-views-container"]/div/div/div/div[1]/div/div/div/div[1]/div/div[1]/div/div[2]/div/div/div/div/div[4]')))
This results in an error whereby the timeout is reached. It is not an issue of the timeout being too short. I have tried the div search for the given text, but that also did not work. Here is one example of the search I tried which was nearly copied from a similar question:
Select(driver.find_element_by_xpath("//div[contains(text(), 'Unit Price: Low First')]"))
Here is the HTML for the desired menu item:
<div role="menuitem" tabindex="0" data-radium="true" style="background-color: rgb(247, 247, 247); padding: 8px 16px; font-size: 16px; font-weight: normal; color: white; white-space: nowrap; line-height: 20px; min-width: 260px; height: 36px; cursor: pointer;">
<div data-radium="true" style="float: left; display: block; line-height: 24px;">Unit Price: Low First</div>
</div>
Ok so that was a bit of a painful lesson in html and Selenium. Anyway, I figured it out by creating a list of all the menu items and then iterating through that list and clicking on the one I wanted. At the moment, I have this:
menu_items=driver.find_elements_by_xpath('//div[#role="menuitem"]')
for i in menu_items:
if "Unit Price" in i.text:
i.click()
break
For some reason I cannot just do menu_items[4].click(), but I believe it is due to the elements list being a list of tuples or something, though I will have to check the documentation to verify this. I will update the answer afterwards.

Scraping IE with VBA, how to scroll to bottom of page?

I am pretty new to VBA. I'm trying to scroll to the bottom of a web page. Below is contents of web page.
<DIV id="Table">
<div style="width: 18px; overflow: auto; height: 912px;" onscroll="Grids[0].Scrolled();" onmousemove="Grids[0].ARow=null;Grids[0].ASec=-1;"><div style="width: 1px; overflow: hidden; height: 1954px;"> </div></div>'
This is VBA Code I have so far.
Dim selector As String
selector = "#Table div[style*='onmousemove']"
Dim rightWindowDiv As HTMLDivElement
Set rightWindowDiv = htmldoc.querySelector(selector)
rightWindowDiv.ScrollTop = rightWindowDiv.ScrollHeight
Somehow it does not work. Can someone help?

How to set the width of suggest boxes in RedQueryBuilder

After you have chosen a field name, then you start typing in the value textbox, the dropdown defaults to a width of 150px which causes lots of things to be cut off. A scrollbar shows up so you can scroll left and right, but I would like to figure out how to set the minimum width of this element.
The css path to the element defining this is:
body > div.gwt-SuggestBoxPopup > div > table > tbody > tr.suggestPopupMiddle > td.suggestPopupMiddleCenter > div > div
It seems to be an inline style in the element
<div class="" style="overflow: auto; position: relative; zoom: 1; height: 322px; width: 150px;">
The field dropdown seems to re-size dynamically, so it does not get scroll bars, the suggest box starts at 150px and resizes sometimes, but it doesn't seem to have any rhyme or reason.
I added the following to my stylesheet:
.suggestPopupMiddleCenter > div > div
{
min-width: 300px; !important
}
But that also affects the field dropdown, which appears to be working properly.
Is this a bug, or is there a configuration that I can use that will allow the value suggestion box dropdown to dynamically resize based on the widest element?
It is hard wired in the tardis branch https://github.com/salk31/RedQueryBuilder/blob/tardis/redquerybuilder-core/src/main/java/com/redspr/redquerybuilder/core/client/expression/SuggestEditorWidget.java#L107
It probably could use some rule to choose the width. Best if you raise a bug and make a suggestion?

Resources