Skip to main content
 首页 » 编程设计

python之如何使用 Selenium 的 webdriver 和 Python 检查网页内容是否已更改

2025年05月04日31傻小

以 20 秒的时间间隔比较 old_page_source 和 new_page_source 对我来说是不成功的。

# using google chrome as my browser 
driver = webdriver.Chrome('chromedriverfilepath') 
 
# 5 trials to see how often page gets updated. Currently unsuccesful 
for x in range(1, 5): 
    # the webpage being analyzed 
    driver.get("www.somewebsite.com") 
 
    old_page_source = driver.page_source 
 
    print time.strftime("\n\nTRIAL %d" % x + " ,first page fetched at time...." + 'Time: %H:%M:%S') 
 
    driver.get("www.somewebsite.com") 
    new_page_source = driver.page_source 
 
    # keep checking every 20 seconds until page is updated/changed 
    while old_page_source == new_page_source: 
        sleep(20) 
        driver.get("www.somewebsite.com") 
        new_page_source = driver.page_source 
 
print "page was changed at time.... " + time.strftime('Time: %H:%M:%S') 

请您参考如下方法:

您所做的事情不能依赖于page_source。 Selenium 将报告的内容很可能是浏览器首先收到的内容。作为文档 mention :

Get the source of the last loaded page. If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page. Please consult the documentation of the particular driver being used to determine whether the returned text reflects the current state of the page or the text last sent by the web server. The page source returned is a representation of the underlying DOM: do not expect it to be formatted or escaped in the same way as the response sent from the web server. Think of it as an artist's impression.

(强调我的。文档是针对 Java 绑定(bind)的,但行为不是由 Java 绑定(bind)决定的,而是由浏览器端的 Selenium 部分决定的。因此这也适用于 Python 绑定(bind)。)

要获取页面的实际状态,您应该做的是:

driver.execute_script("return document.documentElement.outerHTML") 

这将为您提供整个页面的 DOM 树的序列化。