Get HTML Source of WebElement in Selenium WebDriver using Python

后端 未结 14 1767
误落风尘
误落风尘 2020-11-22 13:45

I\'m using the Python bindings to run Selenium WebDriver:

from selenium import webdriver
wd = webdriver.Firefox()

I know I can grab a webel

14条回答
  •  死守一世寂寞
    2020-11-22 14:08

    Sure we can get all HTML source code with this script below in Selenium Python:

    elem = driver.find_element_by_xpath("//*")
    source_code = elem.get_attribute("outerHTML")
    

    If you you want to save it to file:

    with open('c:/html_source_code.html', 'w') as f:
        f.write(source_code.encode('utf-8'))
    

    I suggest saving to a file because source code is very very long.

提交回复
热议问题