1

I am new to python/selenium and i try to do a webscrapper.I am trying to get the names of the teams out of this. This is the code I am using:

url_getsbet = 'https://online.getsbet.ro/sports'


driver = webdriver.Chrome()

driver.get(url_getsbet)

matches_sb = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "span.Details__Participants")))
print(matches_sb)

and this is the html where the element is located

<span class="Details__Group">
  <span class="Details__Participants">
    <span class="Details__Participant Details__Participant--Home">
      <span class="Details__ParticipantName">Benfica</span>
    </span>
    <span class="Details__Participant Details__Participant--Away">
      <span class="Details__ParticipantName">Club Brugge</span>
    </span>
  </span>
  <div class="Score Score--1"></div>
</span>

And it causes this error: line 95, in until raise TimeoutException(message, screen, stacktrace).

I don't realise what's causing this error.

Thank you and you help would be much appreciated!

1 Answers1

1

The desired elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the visibility_of_all_elements_located().

  • You can use either of the following locator strategies:

    • Using CSS_SELECTOR and text attribute:

      driver.get("https://online.getsbet.ro/sports")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.cookie_consent_btn"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#SportsBookIframe")))
      print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span.Details__Participants")))])
      driver.quit()
      
    • Console Output:

      ['Tottenham\nAC Milan', 'Bayern Munich\nPSG', 'Benfica\nClub Brugge KV', 'Chelsea\nBorussia Dortmund', 'Lazio\nAZ Alkmaar', 'UTA Arad\nFcsb', 'Petrolul\nUniversitatea Craiova', 'Voluntari\nFC Argeș', 'CS Mioveni\nChindia Târgoviște', 'Sepsi\nCFR Cluj', 'SCM Gloria Buzau\nFC Unirea 2004 Slobozia', 'Steaua București\nBaia Mare', 'Brentford\nFulham', 'Liverpool\nMan Utd', 'Nottm Forest\nEverton', 'Osasuna\nCelta Vigo', 'Rayo Vallecano\nAthletic Bilbao', 'Real Betis\nReal Madrid', 'Real Valladolid\nEspanyol', 'Barcelona\nValencia', 'Montpellier\nAngers', 'Rennes\nMarseille', 'Troyes\nMonaco', 'Reims\nAC Ajaccio', 'Lyon\nLorient']
      
    • Using XPATH and get_attribute("textContent"):

      driver.get("https://online.getsbet.ro/sports")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='cookie_consent_btn']"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='SportsBookIframe']")))
      print([my_elem.get_attribute("textContent") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='Details__Participants']")))])
      driver.quit()
      
    • Console Output:

      ['TottenhamAC Milan', 'Bayern MunichPSG', 'BenficaClub Brugge KV', 'ChelseaBorussia Dortmund', 'LazioAZ Alkmaar', 'UTA AradFcsb', 'PetrolulUniversitatea Craiova', 'VoluntariFC Argeș', 'CS MioveniChindia Târgoviște', 'SepsiCFR Cluj', 'SCM Gloria BuzauFC Unirea 2004 Slobozia', 'Steaua BucureștiBaia Mare', 'BrentfordFulham', 'LiverpoolMan Utd', 'Nottm ForestEverton', 'OsasunaCelta Vigo', 'Rayo VallecanoAthletic Bilbao', 'Real BetisReal Madrid', 'Real ValladolidEspanyol', 'BarcelonaValencia', 'MontpellierAngers', 'TroyesMonaco', 'RennesMarseille', 'ReimsAC Ajaccio', 'LyonLorient']
      
  • Note : You have to add the following imports :

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

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352