问题
I'm trying to scrape data from this website and I need to select value from picture (in this case "Москва, Новорязанское ш."):

I am having trouble identifying the correct "class" to use and also not sure of which functions can be leveraged to target the specific values I'm trying to keep. Any guidance would be greatly appreciated.
Code that I have:
Public Sub Selected()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
.document.getElementsByClassName("store-switcher__current-store-i").querySelector("[shop='Воронеж']").Click
Stop
End With
End Sub
回答1:
If you examine the HTML of the page you will see each shop has its own data-id
attribute value. You can select and click the appropriate shop name node by the value of this attribute:
Option Explicit
Public Sub MakeShopSelection()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
.document.querySelector("[data-id='36']").Click
While .Busy Or .readyState <> 4: DoEvents: Wend
Stop
.Quit
End With
End Sub
The following will list the ids for you:
Option Explicit
Public Sub ListIds()
Dim ie As SHDocVw.InternetExplorer, idDict As Object, shopNames As Object
Set ie = New SHDocVw.InternetExplorer
Set idDict = CreateObject("Scripting.Dictionary")
With ie
.Visible = True
.navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
Dim i As Long
With .document
Set shopNames = .querySelectorAll(".shop__name")
For i = 0 To shopNames.Length - 1
idDict(Trim$(shopNames.Item(i).innertext)) = shopNames.Item(i).getAttribute("data-id")
Next
End With
With ThisWorkbook.Worksheets("Sheet1")
.Cells(1, 1).Resize(idDict.Count, 1) = Application.Transpose(idDict.keys)
.Cells(1, 2).Resize(idDict.Count, 1) = Application.Transpose(idDict.items)
End With
.Quit
End With
End Sub
来源:https://stackoverflow.com/questions/59117656/select-html-item-using-vba