from selenium import webdriver
driver = webdriver.Chrome()
driver.set_page_load_timeout(7)
def urlOpen(url):
try:
driver.get(url)
print(driver.current_url)
except:
driver.delete_all_cookies()
print("Failed")
return
urls = ['http://motahari.ir/', 'https://facebook.com', 'https://google.com']
for url in urls:
urlOpen(url)
实际上,这个问题是Python Selenium. How to use driver.set_page_load_timeout() properly?。
上面修改后的代码通常运行得很好,但奇怪的是,大约10次delete_all_cookies()
中有2次抛出timeoutException
。我尝试在try-catch
之前添加另一个time.sleep
块,隐式/显式等待或delete_all_cookies()
,但都无效。
我不知道为什么以及如何delete_all_cookies()
抛出timeoutException
...有什么方法可以阻止它吗?
Traceback (most recent call last):
File "C:/urlOpen.py", line 19, in <module>
urlOpen(url)
File "C:/urlOpen.py", line 12, in urlOpen
driver.delete_all_cookies()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 692, in delete_all_cookies
self.execute(Command.DELETE_ALL_COOKIES)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64)
有时,我得到
Traceback (most recent call last):
File "C:/urlOpen.py", line 18, in <module>
urlOpen(url)
File "C:/urlOpen.py", line 11, in urlOpen
driver.delete_all_cookies()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 692, in delete_all_cookies
self.execute(Command.DELETE_ALL_COOKIES)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64)