如何避免在Selenium中触发TimeoutException(message, screen, stacktrace)?

huangapple go评论95阅读模式
英文:

How to avoid raise TimeoutException(message, screen, stacktrace) in selenium?

问题

以下是代码的翻译部分:

我是新手学习Python和Selenium我尝试编写一个网页爬虫我试图从中获取球队的名称这是我使用的代码

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)

这是HTML中元素的位置:

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

这导致了以下错误:
第95行,直到
raise TimeoutException(message, screen, stacktrace).

我不明白是什么导致了这个错误。

谢谢,您的帮助将不胜感激!

英文:

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 = &#39;https://online.getsbet.ro/sports&#39;


driver = webdriver.Chrome()

driver.get(url_getsbet)

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

and this is the html where the element is located

&lt;span class=&quot;Details__Group&quot;&gt;
  &lt;span class=&quot;Details__Participants&quot;&gt;
    &lt;span class=&quot;Details__Participant Details__Participant--Home&quot;&gt;
      &lt;span class=&quot;Details__ParticipantName&quot;&gt;Benfica&lt;/span&gt;
    &lt;/span&gt;
    &lt;span class=&quot;Details__Participant Details__Participant--Away&quot;&gt;
      &lt;span class=&quot;Details__ParticipantName&quot;&gt;Club Brugge&lt;/span&gt;
    &lt;/span&gt;
  &lt;/span&gt;
  &lt;div class=&quot;Score Score--1&quot;&gt;&lt;/div&gt;
&lt;/span&gt;

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

得分: 1

期望的元素位于一个 <iframe> 内,因此您需要:

您可以使用以下任何一种 定位策略

  • 使用 CSS_SELECTORtext 属性:

    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()
    
  • 控制台输出:

    ['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']
    
  • 使用 XPATHget_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()
    
  • 控制台输出:

    ['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', 'RennesMarseille', 'TroyesMonaco', 'ReimsAC Ajaccio', 'LyonLorient']
    
  • 注意:您需要添加以下导入:

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

参考

您可以在以下讨论中找到一些相关信息:

英文:

The desired elements are within an &lt;iframe&gt; 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(&quot;https://online.getsbet.ro/sports&quot;)
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, &quot;button.cookie_consent_btn&quot;))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,&quot;iframe#SportsBookIframe&quot;)))
      print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, &quot;span.Details__Participants&quot;)))])
      driver.quit()
      
    • Console Output:

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

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

      [&#39;TottenhamAC Milan&#39;, &#39;Bayern MunichPSG&#39;, &#39;BenficaClub Brugge KV&#39;, &#39;ChelseaBorussia Dortmund&#39;, &#39;LazioAZ Alkmaar&#39;, &#39;UTA AradFcsb&#39;, &#39;PetrolulUniversitatea Craiova&#39;, &#39;VoluntariFC Argeș&#39;, &#39;CS MioveniChindia T&#226;rgoviște&#39;, &#39;SepsiCFR Cluj&#39;, &#39;SCM Gloria BuzauFC Unirea 2004 Slobozia&#39;, &#39;Steaua BucureștiBaia Mare&#39;, &#39;BrentfordFulham&#39;, &#39;LiverpoolMan Utd&#39;, &#39;Nottm ForestEverton&#39;, &#39;OsasunaCelta Vigo&#39;, &#39;Rayo VallecanoAthletic Bilbao&#39;, &#39;Real BetisReal Madrid&#39;, &#39;Real ValladolidEspanyol&#39;, &#39;BarcelonaValencia&#39;, &#39;MontpellierAngers&#39;, &#39;TroyesMonaco&#39;, &#39;RennesMarseille&#39;, &#39;ReimsAC Ajaccio&#39;, &#39;LyonLorient&#39;]
      
  • 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:

huangapple
  • 本文由 发表于 2023年3月4日 08:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定