catching another value in same <tr in web scraping vba

Viewed 56

What I try to do. I have a team name, I want to scrape from table the WIN stats of that team. What I do, I go through every //tr//span element in table, with if condition try to catch if the team name is in table, if yes, then inside loop, I want to goBack with Selenium xPath go to parent element (..). I wonder how in other way way I can achieve it. Maybe only way is bringing all data in <td inside list, then search inside list not inside HTML. As it seems impossible to go back from element (in my situation <span) you found team name, then going back and taking the 6 th <td value of that same <tr

Sub nbaScrape_Stats()

    Dim driver As WebDriver
    Set driver = CreateObject("Selenium.WebDriver")
    
    Dim xUrl As String
    xUrl = "https://www.nba.com/stats/teams/traditional"
        driver.Start "chrome", xUrl
        
        driver.Get "/", 10000
        
        Dim we_to_click As WebElement
        'https://www.nba.com/stats/teams/traditional?sort=W_PCT&dir=-1&Season=2021-22&SeasonType=Regular+Season&LastNGames=1/
        Set we_to_click = driver.FindElementByXPath("//*[@id=""onetrust-accept-btn-handler""]")
        Debug.Print driver.FindElementByXPath("//*[@id=""onetrust-accept-btn-handler""]").Text, "-date:-", Now()
        we_to_click.Click
        driver.Wait 1000

        Set we_to_click = driver.FindElementByXPath("//section[1]/div/div/div[4]/label/div/select/optgroup[1]/option[1]")
        Debug.Print driver.FindElementByXPath("//section[1]/div/div/div[4]/label/div/select/optgroup[1]/option[1]").Text, "-date:-", Now()
        we_to_click.Click 'run-time error sometimes
        
        Dim wr_to_xl As WebElements
        Set wr_to_xl = driver.FindElementsByXPath("//tbody/tr//span") 'LINE: array of <span elements (1)
        
        i = 1
        For Each element In wr_to_xl
            ThisWorkbook.ActiveSheet.Cells(i, 1).Value = element.Text
                If ActiveSheet.[C3].Value = element.Text Then 'team name if equals
                Set element_main = element.FindElementByXPath("//tbody/tr//span/..") '(2)
                'line below, I want to catch the sixth <td of that row/team's win streak
                    Debug.Print element_main.FindElementByXPath("//table/tbody/tr/td[6]").Text, "<-", element.Text, "-match:-", Now()
                End If
            i = i + 1
        Next
        
        ThisWorkbook.ActiveSheet.UsedRange.Columns.AutoFit
        driver.Quit
End Sub

https://www.nba.com/stats/teams/traditional

First I search for team name, then I took the stats like (WIN% , game played) enter image description here

0 Answers
Related