Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
anonima2008 pe Simpatie
Femeie
22 ani
Bacau
cauta Barbat
22 - 43 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [EXCEL] Using Microsoft Edge Webdriver and VBA to Scrape the Web Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
Mrrrr
AdMiN

Inregistrat: acum 17 ani
Postari: 2228
I am trying to extract information from a webpage that shows distances between cities by car, by inserting the cities from Excel cells into the webpage, then extracting the resulted distance back into Excel.

I was not able to click the button on the following webpage via normal FindElementByClass or Id or whatever.
Here is the link in text form: https://gps-online.webshow.ro/planificator-rute.html
Here is the link in link form:

So I asked ChatGPT to provide alternatives, and JavaScript injection was golden! Also, extracting the text with XPath was a great solution and finally, using RegEx to extract the number from the text, simplified things in Excel.

The code below can be improved of course, for example to go through a range of cities and extract distances between them, but this is the simple - Selection wise - way to go:


Sub Extract_Routes()

' FIRST: Set a reference to Selenium Type Library (Tools > References > Selenium Type Library)
Dim driver As New WebDriver
Dim url As String
    url = "https://gps-online.webshow.ro/planificator-rute.html"

' Start a new Edge browser session and wait 2 secons for the page to load
driver.Start "Edge", ""
driver.Get url
driver.Wait 2000

Dim rng As Range
Set rng = Selection

Dim c1, c2 As String
    c1 = rng.Offset(0, 1).Text
    c2 = rng.Offset(0, 2).Text

' Insert data from cells into "plecare" and "sosire" fields
driver.FindElementById("plecare").SendKeys (c1)
driver.FindElementById("sosire").SendKeys (c2)

' Use JavaScript injection to click the button by its text
driver.ExecuteScript "document.querySelector('button[onclick=""generateRoute()""]').click();"

driver.Wait 3000  ' Wait for 3 seconds, otherwise there will be an "Out of memory" error

' Find the <p> element containing the distance text using XPath
Dim distanceElement As WebElement
Set distanceElement = driver.FindElementByXPath("//div[@id='directions-step-by-step']//p[contains(text(), 'Distanta:')]")

If Not distanceElement Is Nothing Then
    distanceText = distanceElement.Text
   
    ' Extract the numeric value using regular expressions
    Dim regex As Object
    Set regex = CreateObject("VBScript.RegExp")
        regex.Global = True
        regex.IgnoreCase = True
        regex.Pattern = "\d+(?:\.\d{3})*" '"\d+"
' Match one or more digits
   
    ' Find and print the numeric value
    If regex.test(distanceText) Then
        Dim matches As Object
        Set matches = regex.Execute(distanceText)
       
        Dim numericValue As String
            numericValue = Replace(matches(0).Value, ".", ",")
       
        ' Print the first match (the numeric value)
        ' Debug.Print matches(0).Value
        rng.Offset(0, 3).Value = numericValue
    Else
        Debug.Print "Numeric value not found."
    End If
Else
    Debug.Print "Distance text not found."
End If

driver.Quit
   
End Sub


_______________________________________


pus acum 1 an
   
Pagini: 1  

Mergi la