get_attribute 中的硒

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

我希望这样输出

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个字符的项目

你好像只是拼错了变量。

这个:print(ItemsRightList.get_attribute('alt'))

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


编辑:问题出在//tr。一种方法是在下面我们根据 count 变量更改 tr 的地方。

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

逐行工作:

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