使用Python的Selenium可以加载页面

时间:2016-09-30 12:59:42

标签: python selenium selenium-chromedriver

我正在尝试使用简单的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

1 个答案:

答案 0 :(得分:1)

我不确定那个页面在做什么,但它肯定是非典型的。我最好的建议是设置页面加载超时,然后处理相关的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调用返回,因此某些页面功能可能会从根本上被打破。