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

标签 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")

我正在使用 google chrome 驱动程序和 python3。只是为了清楚起见。

编辑1:

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

最佳答案

self.driver.find_element_by_xpath("//body").get_attribute('outerHTML')

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

关于python - 使用 Selenium 获取渲染页面的当前 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48954847/

相关文章:

python - 对于成对求和,我需要多少项才能得到明显错误的结果?

python - webdriver 在 python 中等待 ajax 请求

javascript - 它是当前运行脚本的最后一个 `script` 元素吗?

jquery - 将多行文本与元素分开

python - 提高 float 除法的准确性(python)

python 3 : How to properly add new Futures to a list while already waiting upon it?

java - 如何在 Selenium Webdriver 中模拟鼠标双击

测试失败时 Docker 构建 : returned a non-zero code: 1,

javascript - Bookmarklet 打开一个空的选项卡/窗口,里面有一些 javascript?

python - 确定表达式中是否按下了键 (Python) (PyQT)