简体   繁体   中英

Python Selenium find element and return HTML tags

I am using Python Selenium to find an element within the following HTML...

<div id="results">
    <h1>Results</h1>
    <p>These Are The Results</p>
    <ul>
        <li>Result 1</li>
        <li>Result 2</li>
        <li>Result 3</li>
        <li>Result 4</li>
    </ul>
</div>

result = driver.find_element_by_css_selector("#results").text

This works correctly but the data it returns does not include the HTML tags, is there a way I can make it return the HTML tags as well as the data with the #results div?

In order to get HTML, you have to get attribute 'outerHTML' or 'innerHTML'. The outerHTML will include HTML of current element. See code below:

htmlText = driver.find_element_by_css_selector("#results").get_attribute("outerHTML")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM