python webriver timout和try / except

时间:2016-10-27 10:22:27

标签: python selenium timeout try-catch

我正在尝试打开指向mp3的链接,然后将其关闭。为此,我使用driver.get(url)。如果我使用http://www.google.com则可行。但是当我使用mp3链接时它停留在driver.get(url)。所以它不会关闭。 我曾尝试使用超时加载,但后来又出现了另一个错误。并且它不会执行剩下的代码。

如果已经阅读了某个地方你可以通过try和except解决这个问题,但我不知道调用了什么错误,所以我不知道我必须使用什么异常。

我使用的代码

import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.set_page_load_timeout(5)
driver.get('https://p.scdn.co/mp3-preview/bcc67a9c1772e28a01be2074b8a4c850c8bc4587');
driver.quit()

我得到的错误

Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
driver.get(' https://p.scdn.co/mp3-preview/bcc67a9c1772e28a01be2074b8a4c850c8bc4587');
File "C:\.....\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 248, in get
self.execute(Command.GET, {'url': url})
File "C:\.....\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\....\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout: cannot determine loading status
from timeout: Timed out receiving message from renderer: -0.002
(Session info: chrome=53.0.2785.143)
(Driver info: chromedriver=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 10.0.14393 x86_64)

1 个答案:

答案 0 :(得分:0)

试一试: driver = webdriver.Chrome() driver. implicitly_wait (10) driver.get('https://p.scdn.co/mp3-preview/bcc67a9c1772e28a01be2074b8a4c850c8bc4587'); driver.quit()

更多信息:

http://selenium-python.readthedocs.io/waits.html