我正在尝试使用简单的 Python/Selenium 脚本读取页面
# encoding=utf8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import datetime as dt
import codecs
import os
myDriver=webdriver.Chrome()
myDriver.get("http://spb.beeline.ru/customers/products/mobile/tariffs/")
print "Test"
myDriver.quit()
现在,如果我使用谷歌浏览器打开该网址,页面就会加载,仅此而已。 通过此脚本执行此操作时,页面仍处于加载状态,并且脚本无法进一步执行。
我使用的是 Windows 7,使用 Python 2.7.12、Selenium 2.53.6 和 chromedriver 2.24.41.74.31
我不确定该页面在做什么,但它肯定是非典型的。我最好的建议是设置页面加载超时,然后处理关联的 TimeoutException:
# encoding=utf8
from __future__ import print_function
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
myDriver=webdriver.Chrome()
try:
# Set the page load timeout
myDriver.set_page_load_timeout(10)
try:
myDriver.get("http://spb.beeline.ru/customers/products/mobile/tariffs/")
except TimeoutException:
print("Page expired")
# Do stuff here
finally:
myDriver.quit()
缺点是(我认为)这会杀死后台发生的任何阻止
driver.get
调用返回的事情,因此某些页面功能可能会从根本上被破坏。
为此,我们可以使用environment.py 文件。它的实现就像之前的场景之后的场景一样,毕竟之前或者你能做的就是你可以手动添加:
driver.quit()
在代码的最后一行。