首页 > 解决方案 > 如果我加载视频,python selenium 会给我 TimeoutException

问题描述

我尝试创建一个抓取整个网站的功能。今天得到一个 TimeoutException ......

Traceback (most recent call last):
  File "D:/Entwicklung/example/crawler/crawler.py", line 46, in crawl
    driver.get(tmp)
  File "C:\Users\test\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\test\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\test\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
  (Session info: chrome=75.0.3770.142)

当我将视频的 url 传递给 driver.get() 函数时,会触发 TimeoutException。我的循环一直在运行,但是 TimeoutException 之后的每个 driver.get() 调用也会触发 TimeoutException。为什么会这样?

while len(diff) > 0:
    tmp = diff.pop()
    visited.add(tmp)
    driver.get(tmp)

    elements = driver.find_elements_by_tag_name("a")

    for element in elements:
        href = element.get_attribute('href')
        if href is None:
            continue
        else:
            if main_url in href:
                links.add(href)

    diff = links.difference(visited)

标签: pythonseleniumselenium-webdriverselenium-chromedriver

解决方案


您的代码正在达到此默认超时,这就是您看到该消息的原因。

您是否尝试过使用Waits?他们在进入下一个代码块之前给了 Selenium 更多的时间。

找到另一个博客:处理 Selenium 超时。他们修改了 Selenium 的默认超时设置。


推荐阅读