Extract item price from HTML - html

I'm struggling to get the price from the following HTML
<span class="pricing__now" itemprop="price">7.99</span>
I'm trying to extract the "7.99" from the above example.
I've tried HTML.getElementsByClassName("pricing__now")(0).innertext
but drawing blank.
Any help, kindly received.
Many thanks in advance.
Ian

It will be easier to give better advice if we can see more HTML and also the URL if possible. Here are some tips:
1) You need to ensure you have the right index
To check you have the right index, right click inspect element the press Ctrl + F to bring up the HTML search box and enter .pricing__now. Check how many matches there are and at which index your desired match is.
2) Ensure the page has loaded with this value when you try to access. So before trying to access, if using IE, ensure you have:
While IE.Busy Or IE.readyState < 4: DoEvents: Wend
after your navigate2 line
3) You can also use combined CSS selectors to be more specific
ie.document.querySelector(".pricing__now[itemprop=price]").innerText

Related

Internet Explorer click an icon and a button of a specific line of a table

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

How to click on "Submit" button from a website

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

VBA: Click option on dropdown list from website

Am sorry if it may be very simple, I am a newbie, but I have researched a lot without finding how to click this option (XBT/USD) inside the dropdown list:
https://ibb.co/jqf7zk
I only have managed to display the list with the code below, but I don't know how to select XBT/USD because this doesn't have an ID on html source.
Option Explicit
Sub BrowseToSite()
Dim IE As New SHDocVw.InternetExplorer
Dim oSelect As HTMLInputButtonElement
IE.Visible = True
IE.Navigate "https://www.kraken.com/charts"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
IE.Document.getElementById("pairselect-button").Click
End Sub
HTML code according to inspect element:
< a tabindex="-1" class="currpairs" data-pair-text="XBT/USD" data-pair="XBTUSD">XBT/USD</a >
Thanks in advance for your valuable response.
You are almost there. You have found the dropdown menu. All you need to do is clicking XBT/USD. The easiest method that comes to my mind is using getElementsByClassName but you dont have to necessarily use it. You can find them by using xpath or tagname as well.
Well, if you inspect the source you will see there are lots of class="currpairs". If you hover on them, you will see that they belong to each item in dropdown list. XBT/USD is the second item in the list. So the missing part in your code is:
IE.Document.getElementsByClassName("currpairs")(1).Click
Notice we used (1) after getting the class currpairs. This is because counting starts from 0 in list. So (0) represents the first item XBT/EUR, and (1) represents the second item XBT/USD in the list.
Hope this helps.

Referencing the value of a hyperlinked text box

So I'm having some difficulties with this code. I know it's obnoxiously wordy, but every attempt I made to turn some of these form references into variables to save space ended with me having even less functionality than before.
Basically what I've done so far is create a navigation form with several tabs, one to create a ticket, one to resolve/edit a ticket, and one to search the tickets. The search tab is basically a continuous form that updates based on the search criteria I enter. My goal is that when I click on the ticketID for each record, it will take me to the selected record on the Resolve/Edit Ticket page (on that page I have a combo box [called cboGoToRecord] where you can select the record you want).
I have a hyperlink in place that takes the user to the Resolve/Edit page and code that works ONLY when the line I've denoted with four asterisks (for clarity) is replaced with
rst.FindFirst "ticketID =" & [some number].
When I do that, the results are as expected. If I leave it as it is below, every record looks up the first record (A Debug.print check shows that the value of this field is apparently always 1...) So I guess what I need to figure out is how do I access the ticketID hyperlink's value so that I can put it on that line and make my code function effectively? I apologize if this is overly detailed but figured too much was better than not enough.
Private Sub ticketID_Click()
'Takes user from Search Tickets to Resolve/Edit Issues tab
DoCmd.BrowseTo acBrowseToForm, "frmResolveIssues", "frmBrowseTickets.NavigationSubform"
On Error Resume Next
Dim rst As Object
Set rst = Forms!frmBrowseTickets!NavigationSubform.Form.RecordsetClone
[Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value = [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
****rst.FindFirst "ticketID =" & [Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value
Forms!frmBrowseTickets!NavigationSubform.Form.Bookmark = rst.Bookmark
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
End Sub
Edit:
After altering my form to add a separate hyperlink and referencing the static ticketID, I have concluded that everything I thought was true was not. Finding the value of a hyperlink was NOT the problem. The problem is that my ticketID value truly does insist on being one, and I have no clue how to fix that.
When this works:
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
then also check out:
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value
As June7, I never use the Navigation form. It complicates everything too much.

Select multiple values in a dropdown listbox on html via VBA

Issue : Hey, I am trying, from VBA,to select multiple values in a dropdown list on a webpage.
The HTML code of the select element is
I managed to select this element with objIE.document.getElementsByTagName("select")(0) since it's the first element with the tagname "select" on the whole webpage.
If I write objIE.document.getElementsByTagName("select")(0).Value= "E31", the value "E31" will be highlighted in the drop box, that's fine.
But how can I select multiple values ?
I tried objIE.document.getElementsByTagName("select")(0).Value= "E31" + "E32" or objIE.document.getElementsByTagName("select")(0).Value= "E31" & "E32" but it doesn't work...
EDIT : Further attempts : I am currently trying to fix this issue using the SendKeys command where I click on one element of the list and then select other elements with SendKeys "+{DOWN 4}" which should select the 4 elements in the list under the initial one selected.
'objIE.document.getElementsByTagName("select")(0).Children(8).Selected = True' If I activate this line it selects (= highlight) the 9th element of the list
'objIE.document.getElementsByTagName("select")(0).Children(8).Click'if I activate this line it clicks on the 9th element of the list but doesn't highlight it
SendKeys "+{DOWN 3}"
Unfortunately this doesn't work yet. Moreover, I don't know if there is a way to send a key CTRL + MouseLeftClick somehow as mentioned in here but for the command Send which is not valid in VBA.
Any ideas ?
Solution : Ok I found a way to do it, pretty easy actually.
With objIE.document.getElementsByTagName("select")(0)
.Children(23).Selected = True
.Children(56).Selected = True
.Children(98).Selected = True
End With
This command will highlight (select) the 3 elements