英文:
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 = '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
得分: 1
期望的元素位于一个 <iframe>
内,因此您需要:
- 使用 WebDriverWait 来等待所需的 frame 可用并切换到它。
- 使用 WebDriverWait 来等待 visibility_of_all_elements_located()。
您可以使用以下任何一种 定位策略:
-
使用 CSS_SELECTOR 和 text 属性:
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']
-
使用 XPATH 和
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()
-
控制台输出:
['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
参考
您可以在以下讨论中找到一些相关信息:
- 通过 Selenium 和 Python 切换到 iframe
- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
- Python 中的 Selenium:NoSuchElementException: Message: no such element: Unable to locate element
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论