Click events execution in VBA data extraction - vba

I am using below mentioned code for button click events,My code for main page is working properly but in below mentioned url one additional page is loading after first page loading so click events are not working properly for add to cart,view cart and cart deletion event,I can't find out a feasible solution for resolution of trialing issue.
My urls is https://www.amazon.com/dp/B097QGD9DL/ref=olp_aod_redir_impl1/130-4397383-6777352?_encoding=UTF8&aod=1 and code is given below.
'----add to cart button click event
Set btn = html9.getElementsByClassName("a-button-input")
btn.Click
Sleep 2000
'Ie.Refresh
Set html5 = Ie.Document
'---view cart button click event
Set btn1 = html5.getElementById("a-button-input")
btn1.Click
Sleep 3000
Set html6 = Ie.Document
html7 = html6.body.innerHTML
clr = html.getElementsByClassName("sc-product-variation")(0).innerText
Size = html.getElementsByClassName("sc-product-variation")(0).innerText
'---------click on delete button to clear add to cart
Set btn3 = html6.getElementsByClassName("a-color-link")
btn3.Click

With my experience with Selenium, may i suggest you to try narrowing down your search area for button.
On second load page, there are almost 23 elements with id "a-button-input". Its a very generic id for button.
My suggestion is to first search for unique container closest to our relevant button. And then try to search for this button in the container only.
Look forward to your response. Your project is very interesting.

Related

Word - how to uncheck checkboxes?

I have 4 checkboxes but we need to restrict selection to just a single one, meaning if you check the first, the other 3 will go unchecked. I know we could use ActiveX radio buttons but we'd prefer to avoid ActiveX if possible, plus with check boxes we have more control over the layout.
I've set the name of the checkbox appropriately to Check1:
And then I've put this very basic script into the Visual Basic section:
Private Sub Check1_Click()
Check1.Enabled = True
Check2.Enabled = False
Check3.Enabled = False
Check4.Enabled = False
End Sub
But unfortunately checking the first box doesn't uncheck the next 3.
Any ideas please? Thank you!
If these are Content Controls, as you indicate, then they do not have a CLICK event. Nor can they be identified by VBA by their Title property. The code you show us is for ActiveX controls, which you say you don't want to use...
Working with content control events is not as simple and intuitive as with ActiveX controls. Similar to form fields, Content Controls only have "editing" events that trigger on the user entering and exiting the content control. These events are available in the ThisDocument module, in the Document category.
The same ContentControlOnExit event triggers for ALL content controls in the document, so you need a Select Case or If conditional to query the ContentControl argument in order to know which content control was exited.
In order to address the other checkboxes you need to use the Document.SelectContentControlsByTitle (or ...ByTag) method which returns an array of all content controls with that title (or tag).
If you really want to emulate a "click" event then you need to insert a Custom XML Part in the document with nodes linked to the content controls. When the user changes the state of the control the ContentControlBeforeStoreUpdate event will trigger, letting you take action.
The property you need is Value, not Enabled.
The purpose of property Enabled is to prevent a control from being changed by user.
Additionaly, you need to prevent it from the events cascade. It means that when you change programatically the value of Check2, this will trigger Private Sub Check2_Click() and so on.
In order to make it work you should change your code like that:
Private Sub Check1_Click()
If Check1.Value Then
Check1.Value = True
Check2.Value = False
Check3.Value = False
Check4.Value = False
End If
End Sub
and similarly for the other check boxes.
For your purpose radio buttons will be better choice. Radio buttons have built-in functionality to uncheck currently selected button if other one is checked.

VB.NET web application - Update a data bound Listbox when underlying table / query changes

I have a page in my web application that contains two listboxes with buttons to move items back & forth between them. Each listbox is bound to a SQL query, the results of which change as the selected items are added or removed from the corresponding lists. This all works fine, except I cannot get the list boxes to update their contents on the fly, however if I refresh the web page, the contents update correctly.
Basically the user selects items in LeftListbox and clicks the Add button which calls code to loop through the LeftListbox and for each selected item adds a new record to a table (Score). The RightListbox should then update to show that the items have been added to the table.
Here is a snippet of code from the Click event of the Add button:
Dim i As Integer
For i = 0 To (LeftListbox.Items.Count() - 1)
If LeftListbox.Items(i).Selected Then
Try
DbUtils.StartTransaction()
Dim rec As ScoreRecord = New ScoreRecord
rec.Player_ID = CInt(Me.LeftListbox.Items(i).Value)
rec.Save()
DbUtils.CommitTransaction()
Catch ex As Exception
DbUtils.RollBackTransaction()
Me.Page.ErrorOnPage = True
Finally
DbUtils.EndTransaction()
End Try
End If
Next i
'** Here is where I want to refresh the list **
I've searched quite a bit for a solution, but I can't find anything that works so any help would be much appreciated.
Andrew
Use the same method (or code) used to populate the "right listbox" in the first place. The right ListBox's DataSource will be the same as it was prior to this code snipped being ran, so it must be updated since the underlying data has changed.

How to check all the links dynamically?

The following thing I have to do using VBScript in QTP 10/11:
The VBScript opens one login page. After the login it counts the number of links in that page and then prints all the links. Afterwards it opens every link one by one.
I am having issues with finding all the dynamic links.
You can get all the links on a page by using the Page's (or Frame's) ChildObject method.
Set desc = Description.Create()
desc("html tag").Value = "a"
Set links = Browser("B").Page("P").ChildObjects(desc)
For i = 0 to links.Count - 1
Print links(i).GetRoProperty("inner_text") & " => " & links(i).GetRoProperty("href")
Next
As for clicking them, that's a bit more complicated since after clicking a link you cause a navigation that invalidates the links object, you should either perform the ChildObjects each time (while keeping track of the index) or open the links in a different browser/tab.

VB.net - Issues with reading radio buttons between forms

I have a basic math test program, and 2 forms to allow the user to choose which function to test on.
The first form has a list of radio buttons with the 4 basic math functions and a button to load the next form.
The second form contains this code on Load...
'Change function sign to reflect chosen option
If frmOptions.rdoAdd.Checked Then
lblFunc.Text = "+"
ElseIf frmOptions.rdoSub.Checked Then
lblFunc.Text = "-"
ElseIf frmOptions.rdoMult.Checked Then
lblFunc.Text = "x"
ElseIf frmOptions.rdoDiv.Checked Then
lblFunc.Text = "÷"
End If
If I change the Checked field in the properties in form 1, it passes to form 2.
However if I use the radio buttons to choose a different option, form 2 sticks with the default choice from form 1.
EDIT
Changed a few lines based from input, but still having this issue.
I'll run through it again.
frmOptions has 4 radio buttons, addition is default chosen.
After selecting one, the user clicks a button to open frmTest.
The above code runs upon first load to change a label to reflect what was chosen on frmOption above.
On step 3, the label on frmTest that is supposed to be changed based on the Checked radio button from frmOption never changes. It doesn't even change on the first load of the form, so I think something is wrong with the Checked property of the radio buttons.
Hopefully this clears things up a bit.
It seems like there's not enough information here to know for sure.
It sounds like your first form is loading the second form with values the first time, but then on subsequent calls it doesn't load the second form because it's already loaded. Does that sound correct?
If so, then that you are probably setting values on the first form which get passed to the second form. Instead, create properties on the second form and load it first. Then have the first form manipulate those properties.
Try firing an event when the radio button changes instead of when form2 loads.
Try using the Form2.Shown event instead of Form2.Load.
A form is loaded only once, but it can be shown numerous times.
Have you tried
If My.Forms.Form1.Radiobutton_Name.Checked = True Then
lblFunc.Text = "+"
ElseIf My.Forms.Form1.Radiobutton_Name.Checked = True Then
lblFunc.Text = "-"
ElseIf My.Forms.Form1.Radiobutton_Name.Checked = True Then
lblFunc.Text = "x"
ElseIf My.Forms.Form1.Radiobutton_Name.Checked = True Then
lblFunc.Text = "÷"
End If

Access command from the Quick Access Toolbar

When I use a macro, that opens a new email from a template, I would also like to access one of the commands from the Quick Access Toolbar and make sure a particular option is selected from the drop-down list on this command.
For example, the tab on the toolbar is called 'Add-Ins' and within this tab the area on the toolbar is called 'custom toolbars'. There is a command in this section entitled 'ACT! History', and I would like to automatically chose 'None' from the drop-down list for this command. I do have a screen grab of the toolbar I can send you if this helps.
The current macro is a very simple one as the text below shows.
Sub MakeItem()
Set newItem = Application.CreateItemFromTemplate("C:\Users\jfield\AppData\[file location]\New Placement!.oft")
newItem.Display
Set newItem = Nothing
End Sub