我正在努力获取 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/