I'm new to VBA and have stumbled upon this one problem where I coultnt use vba code to automatically click on a "Submit" button from a website. I have tweaked my code many times but it always skipped the line "e.click". Below is my recent code and an image of the website's elements.
Hope someone can shed some lights here.
Set tags = objIE.Document.getElementById("alltab").getElementsByTagName("a")
For Each e In tags
If e.getAttribute("alt") = "Submit a Contract" Then
e.Click
End If
next
website's elements
You can simply use an attribute = value selector for the alt attribute. Nice and fast. You don't want to loop an entire collection if you don't have to. Also, in any loop you would want to Exit For after found I believe.
objIE.document.querySelector("[alt='Submit a Contract']").click
Please check your code, from your screenshot it seems that the hyperlink (html a tag) not inside the alltab table.
Please try to find the table by the class name and modify your code to add an ID property:
Find the table by ID property (add an ID property for the second table):
Set tags = doc.getElementById("table id").getElementsByTagName("a")
For Each e In tags
If e.getAttribute("alt") = "Submit a Contract" Then
e.Click
End If
Next e
or
Find the table by class name:
Set tags = doc.getElementsByClassName("belowDealButtonBox")(0).getElementsByTagName("a")
For Each e In tags
If e.getAttribute("alt") = "Submit a Contract" Then
e.Click
End If
Next e
Related
I want to ask a question that how can I click and copy text from the HTML Classes like there are too many classes with the same id. For example there are 10 classes with this code div class="price". I want to copy inner text from all the same "Price" classes one by one.
The copying process will have to be this:
Click and copy inner text which we suppose it's "$300" from the price class. (The page will be redirected to the ad).
Now move back to previous page and click on the another price class and "$300" (Which we clicked and copied) is exception.
By this process I can click and copy text from all the classes which have same div id "Price".
Here I've screenshot, see these are the ads which I want to click one by one!
I'll be very thankful if someone provides me an answer for this question.
PS: Thanks in advance!
What I have tried:
I can click and copy the "price" class but the next time when I try to click on the next class which has the same id "Price", I'm unable to do this.
Edit:
This is the code I tried:
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element.GetAttribute("className") = "Price" Then
For Each Element1 As HtmlElement In Element.GetElementsByTagName("div")
If Element1.GetAttribute("className") = "Price" Then
Dim Variable1 as String = Element1.InnerText
End If
Next
End If
Next
I've asked a similar question two days ago but I know stumble again on a similar problem but somehow different. previous question asked on a related problem
I have a report of many lines with the same structure. I need to click an icon that is on the nth line. That report is structured in cells so I know that my icon is in the first position (column) of that report. After I have click that icon I'll also have to click on a button in the 10th column.
I already know how to access the page in question with that code
Sub click_button_no_hlink()
Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application") 'create IE instance
IE.Visible = True
IE.Navigate "https://apex.xyz.qc.ca/apex/prd1/f?p=135:LOGIN_DESKTOP::::::" ' Adress of web page
While IE.Busy: DoEvents: Wend 'loading page
This first part is easy isn't? And I know how to handle it. Afterward I tried different variation around this but it either do nothing, or I get an error message. Obviously I don't fully understand what I'm doing with the "querySelector" thing…
dim step_target as string
step_target = 2
'identify all the lines of my table containing lines, containing icons
'and button to click on
Set objCollection = IE.document.getElementsByClassName("highlight-row")
i = 0
Do While i < objCollection.Length
'cell 2 is the one containing the step I'm targetting
If objCollection.Item(i).Cells(2).innerText = step_target Then
'that's not doing anything
objCollection.Item(i).Cells(9).Click
'tried many syntax around this with no luck
IE.document.querySelector([objCollection.Item(i).Cells(9)]).FireEvent ("onclick")
End If
i = i + 1
Loop
Here's images of the code of the page
Showing all the lines of the report
Showing all code lines of a particular line
and now the code of that first icon I need to click on (this is where I need help ;-) how can I call that action)
and finally the code of that button I also need to click on
Again, I thank you all in advance, for the time you'll take to help me along this.
you could try attribute selector for first in combination with descendant combinator and a type selector
ie.document.querySelector("[headers='ID_DET_DEM_TRAV_STD'] a").click
you could try attribute selector for second in combination with descendant combinator and input type selector
ie.document.querySelector("[headers='BOUTON1'] input").click
alternative for second is
ie.document.querySelector("[value=Fait]").click
Typically, if you want to select by position e.g. 1 and 10th columns you would use
td:nth-of-type(1)
td:nth-of-type(10)
Though you would also use a tr:nth-of-type(n) to get the right row as well e.g. first row, first col. Then add in any child type selector, for example, that you might need.
ie.document.querySelector("tr:nth-of-type(1) td:nth-of-type(1)")
Child a tag:
ie.document.querySelector("tr:nth-of-type(1) td:nth-of-type(1) a")
Child input tag: would then be:
IE.document.querySelector("tr:nth-of-type(4) td:nth-of-type(10) input").Click
I figured out how to navigate to the webpage and make it search for my specific input and so fourth. However when i get the list with the result i am not able to make vba click on it. I have tried different variations of:
objIE.document.getElementById("isc_ListGrid_1_body$28s").Click
But I'm unable to figure how to press the specifc cell (marked in blue in pic):
https://imgur.com/inH25WQ
I'm not getting any error's, but its not click on it either.
You want the id of the table, not the id of the div. Then the last td within that table
e.g.
ie.document.querySelector("td:last-child").Click
Try alternatively
Dim evt As Object
Set evt = ie.document.createEvent("HTMLEvents")
evt.initEvent "onclick", True, False
With ie.document.querySelector("td:last-child")
.Focus
.dispatchEvent evt
End With
Or
With ie.document.querySelector("td:last-child")
.Focus
.FireEvent "onclick"
End With
Can someone explain to me why when I click on the following element, the form is not sent. The element is identified however there is no result.
This is the part of the code i am interested in solving, everything up to this point is running fine.
Set IEDoc = Internet.document
Set collection = IEDoc.getElementsByTagName("input") ' there are actually 9 elements
For Each Element In collection
If Element.Type = "image" Then ' there is only the element of interest in here
Element.Click
End If
Next
Here is a sample of the HTML code from the webpage:
Here is a bigger scale of the HTML code from the page in which we can see the form containing the button to submit the information.html code bigger scale
user interface of the webpage
I am trying to find a way to automatically refer to a textbox name within its click event. The name will be used in a sub called by the event code. I know that it can be hard coded, but I have many textboxes and would prefer to have general purpose code. Any help would be appreciated.
EDIT:
Yeah, I should have typed 'label' instead of 'textbox' - sorry. Anyway, I'd like to be able to use a simple bit of code like:
Private Sub lbl_Charges_Click()
tabPage (me.activecontrol.name)
End Sub
Obviously, the activecontrol method does not work on labels. The code I'm calling is:
Private Sub tabPage(page As String)
page = Mid(page, 5)
Echo False
Me.tab_Menu.Pages("pag_" & page).SetFocus
Me.tab_Menu.Visible = True
Me.lbl_Hide.SetFocus
Echo True
End Sub
The reason I'm doing all of this is that I'm trying to find a way to get rid of all of the flicker associated with this (tab menu and label backgrounds are transparent) The echo method will take care of the flicker with the tab menu pages, but doesn't help with the command buttons.
TIA
You can use ActiveControl:
Call SomeClickFunction(Me.ActiveControl.Name)
or, to pass the control itself:
Call SomeClickFunction(Me.ActiveControl)