Search code examples
pythonseleniumfor-in-loop

Selenium for in get_attribute


The url I use https://www.leagueofgraphs.com/match/kr/4320209585#participant6

driver.get("https://www.leagueofgraphs.com/match/kr/4320209585#participant6")

SummonerNameLeft = driver.find_elements_by_xpath("//tr//td[1]/div/div[2]/a/div[1]")
SummonerNameRight = driver.find_elements_by_xpath("//tr//td[6]/div/div[2]/a/div[1]")

ChampionsLeft = driver.find_elements_by_xpath("//tr//td[1]/div/div/a/div/img")
ChampionsRight = driver.find_elements_by_xpath("//tr//td[6]/div/div/a/div/img")

ItemsLeft = driver.find_elements_by_xpath("//tr/td[3]/div//img")
ItemsRight = driver.find_elements_by_xpath("//tr/td[4]/div//img")

KdasLeft = driver.find_elements_by_xpath("//tr/td[2]/div[1]")
KdasRight = driver.find_elements_by_xpath("//tr/td[5]/div[1]")

for count in range(0, 5):
    print(ChampionsLeft[count].get_attribute('alt'), "- Name: ", SummonerNameLeft[count].text, " - ",KdasLeft[count].text)
   for ItemsLeftList in ItemsLeft:
        print(ItemsLeftList.get_attribute('alt'))
    print(ChampionsRight[count].get_attribute('alt'), "- Name: ", SummonerNameRight[count].text," - ", KdasRight[count].text)
    for ItemRightList in ItemsRight:
        print(ItemsRightList.get_attribute('alt'))

I want my output this way

Sample:  (According to the information in the link)
Aatox - Name:  EXP BUNZI  -  1 / 4 / 0 
Ninja Tabi
Kindlegem
Doran's Shield
Caulfield's Warhammer
Phage
Warding Totem (Trinket)

but i cant call the items of 1 character at a time


Solution

  • Work on a row by row basis:

    driver.get("https://www.leagueofgraphs.com/match/kr/4320209585#participant6")
    
    player_rows=driver.find_elements_by_xpath('.//tr[@class="playerRow"]')
    
    for row in player_rows:
        SummonerNameLeft = row.find_element_by_xpath(".//td[1]/div/div[2]/a/div[1]").text
        SummonerNameRight = row.find_element_by_xpath(".//td[6]/div/div[2]/a/div[1]").text
    
        ChampionsLeft = row.find_element_by_xpath(".//td[1]/div/div/a/div/img").get_attribute('alt')
        ChampionsRight = row.find_element_by_xpath(".//td[6]/div/div/a/div/img").get_attribute('alt')
    
        ItemsLeft = row.find_elements_by_xpath(".//td[3]/div//img")
        ItemsRight = row.find_elements_by_xpath(".//td[4]/div//img")
    
        KdasLeft = row.find_element_by_xpath(".//td[2]/div[1]").text
        KdasRight = row.find_element_by_xpath(".//td[5]/div[1]").text
    
        print(ChampionsLeft, "- Name: ", SummonerNameLeft, " - ",KdasLeft)
    
        for ItemsLeftList in ItemsLeft:
            print(ItemsLeftList.get_attribute('alt'))
    
        print(ChampionsRight, "- Name: ", SummonerNameRight," - ", KdasRight)
    
        for ItemRightList in ItemsRight:
            print(ItemRightList.get_attribute('alt'))