Selenium等待页面加载python

时间:2016-11-13 23:39:15

标签: python selenium web-scraping

我目前正在尝试通过python 2.7.12使用selenium驱动程序提交表单,然后检索加载的页面。我目前似乎没有等待页面加载的代码这是我的代码:

while False:
    try:
        driver.find_element_by_id("ctl00_lblStockName") #oldpage id
        break
    except StaleElementReferenceException:
        continue

print ("Page is ready!")
f = driver.page_source
soup = BeautifulSoup(f,'html.parser')
print (soup)

上面的代码只是在搜索之前打印页面的html。我不确定我的while循环是否正常工作。

我尝试了另一种使用超时功能的方法,但错误显示为

AttributeError: 'WebDriver' object has no attribute 'manage'

我也读到使用超时也不是特别可靠,所以我想避免使用这种方法,除非它是唯一的选择。

2 个答案:

答案 0 :(得分:1)

请检查你的代码driver.manage()是否被调用删除函数调用查找python绑定中的替代方法(http://selenium-python.readthedocs.io/api.html)。

driver.manage()。在Selenium的Java Bindings中,不在Python Bindings中。

答案 1 :(得分:0)

对于仍在寻找Python解决方案的任何人:

set_page_load_timeout(*time in seconds*)

例如

driver.set_page_load_timeout(20)     #waits for a page to load within 20 seconds before Selenium throws out Timeout exception