使用 Selenium 获取渲染页面的当前 HTML

Hel*_*101 5 python selenium dom

我正在努力获取 facebook 应用程序的 selenium 中渲染的 html 代码。登录后,我进入应用程序页面并等待time.sleep(20)它完全渲染。

我已经尝试了 3 个解决方案,但没有一个有效:

self.driver.page_source # This one returns source code
self.driver.find_element_by_xpath('//*').get_attribute('outerHTML')
self.driver.execute_script("return document.getElementsByTagName('html')[0].outerHTML")
Run Code Online (Sandbox Code Playgroud)

我正在使用 google chrome 驱动程序和 python3。只是要明确一点。

编辑1:

我不清楚不起作用是什么意思。上述每一项都返回与我点击“检查元素”时所呈现的内容不同的内容

Aph*_*hid 5

self.driver.find_element_by_xpath("//body").get_attribute('outerHTML')
Run Code Online (Sandbox Code Playgroud)

...并且不要使用 time.sleep()。使用 WebdriverWait 并等待页面上的唯一元素加载。