Selenium for in get_attribute

问题描述 投票:0回答:1

我使用的网址 https:/www.leagueofgraphs.commatchkr4320209585#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'))

我希望我的输出是这样的

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)

但我不能同时调用1个角色的物品。

python selenium for-in-loop
1个回答
1
投票

一行一行的工作。

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'))

0
投票

看来你只是拼错了变量。

这个,应该是: print(ItemsRightList.get_attribute('alt'))

应该是..: print(ItemRightList.get_attribute('alt'))


编辑:问题出在//tr. 下面是一种方法,我们在这里改变了 tr 基于 count 可变的。

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")


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, 2):
    print(ChampionsLeft[count].get_attribute('alt'), "- Name: ", SummonerNameLeft[count].text, " - ",KdasLeft[count].text)
    ItemsLeft = driver.find_elements_by_xpath("//tr[{}]/td[3]/div//div//img".format(count + 2))
    for ItemsLeftList in ItemsLeft:
        print(ItemsLeftList.get_attribute('alt'))
    print(ChampionsRight[count].get_attribute('alt'), "- Name: ", SummonerNameRight[count].text," - ", KdasRight[count].text)
    ItemsRight = driver.find_elements_by_xpath("//tr[{}]/td[4]/div//img".format(count + 2))
    for ItemRightList in ItemsRight:
        print(ItemRightList.get_attribute('alt'))
© www.soinside.com 2019 - 2024. All rights reserved.