0

With this code i am trying to extract text from a table called "Last matches" in this webpage

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


url = 'https://s5.sir.sportradar.com/sports4africa/en/1/season/80526/headtohead/334075/340986/match/27195664'
driver = webdriver.Edge("C:/Users/Hama/Documents/msedgedriver.exe")
driver.get(url)
driver.implicitly_wait(10)
WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located((By.XPATH, "//strong[text()='Last matches']/ancestor::div[6]//tbody/tr")))
rows= driver.find_elements_by_xpath("//strong[text()='Last matches']/ancestor::div[6]//tbody/tr")
All_last_matches = []
for res in rows:
   score = res.find_element_by_xpath(".//td[5]//div[@class=' no-wrap']").get_attribute("innerText")
   All_last_matches.append(score)
print(All_last_matches)

It gives me this list:

All_last_matches = ['2:0', '0:4', '3:4', '2:2', '0:1', '3:0', '2:0', '0:4', '1:0', '2:1', '1:1', '1:2']

How can i modify my code to get two lists like this:

Last_matches_team1 = ['2:0', '0:4', '3:4', '2:2', '0:1', '3:0']

Last_matches_team2 = ['2:0', '0:4', '1:0', '2:1', '1:1', '1:2']

I tried this:

Last_matches_team1 = All_last_matches[0:6]

Last_matches_team2 = All_last_matches[6:len(All_last_matches)]

But this will work only if the table1 have 6 rows, sometimes there is just 5 rows (5matches played)

Help is apprciated, thanks to all of you

4
  • 1
    Why don't you check the len first? Commented May 11, 2021 at 14:41
  • Even if i cheek the len of the list it will not help me, because i will not know from which index the result belongs to team2 Commented May 11, 2021 at 14:43
  • Is there no way to differentiate the scores from the structure of the page/HTML? Commented May 11, 2021 at 14:56
  • The code provided isn't mine, thanks to @vitaliis for helping me creating it, i am newbie so that's why i don't know how to resolve this issue Commented May 11, 2021 at 14:59

3 Answers 3

1

As mentioned in previous post, there are 2 tables so you need to treat them separately to get the lists you want

last_matches_team1 = []
last_matches_team2 = []

left_table = "(//table[@class='table'])[1]//tr[@class='cursor-pointer']//td[6]//div[@aria-label='Score']"
for row in WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, (left_table)))):
    score = row.get_attribute("innerText")
    last_matches_team1.append(score)
print(last_matches_team1)

right_table = "(//table[@class='table'])[2]//tr[@class='cursor-pointer']//td[6]//div[@aria-label='Score']"
for row in WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, (right_table)))):
    score = row.get_attribute("innerText")
    last_matches_team2.append(score)
print(last_matches_team2)

Prints:

['2:0', '0:4', '3:4', '2:2', '0:1', '3:0']
['2:0', '0:4', '1:0', '2:1', '1:1', '1:2']
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the following xpath //table[@class='table'] or css_selector table[class='table']
This wil give you exactly the 2 tables you are looking for.
Inside them you can clearly get the scores and put them into separate lists.

Comments

1

Use find_elemnts_by_xpath using xpath //strong[text()='Last matches']/ancestor::div[6]//following-sibling::tbody

This will get you the 2 tables you need. Iterate through the collection by find_element_by_tag_name("tr") to get the respective table rows

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.