我試圖查看是否有評論部分在YouTube視頻上啓用。如果不是,程序應該再次返回頂部。Try/Except無法使用TimeoutException
def bot1():
while True:
# First search
driver.get("https://www.youtube.com/results?sp=CAISAggBUBQ%253D&q=" + k1)
driver.find_element_by_class_name("yt-uix-tile-link").click()
try:
WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.CLASS_NAME, "comment-simplebox-renderer-collapsed-content"))
)
except TimeoutException:
bot1()
這給我的錯誤
Traceback (most recent call last):
File "C:/Users/Admin/Desktop/My Programs/YouTubeTest/YouTubeTest.py", line 98, in <module>
bot1()
File "C:/Users/Admin/Desktop/My Programs/YouTubeTest/YouTubeTest.py", line 73, in bot1
EC.presence_of_element_located((By.CLASS_NAME, "comment-simplebox-renderer-collapsed-content"))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
我得到這個
from selenium.common.exceptions import TimeoutException
我不知道爲什麼會這樣。
你什麼時候可以期待代碼打破? – TerryA
經過WebDriverWait。如果它在10秒內沒有找到評論部分,它會中斷。 –