0

I'm using Python with Selenium.

I am attempting to do some web scraping. I have a WebElement (which contains child elements) that I would like to save to a offline file. So far, I have managed to get the raw HTML for my WebElement using WebElement.get_attribute('innerHTML'). This works, but, no CSS is present in the final product because a stylesheet is used on the website. So I'd like to get these CSS properties converted to inline.

I found this stackoverflow solution which shows how to get the CSS properties of an element. However, getting this data, then parsing the HTML as a string to add these properties inside the HTML tag's style attribute, then doing this for all the child elements, feels like it'd be a significant undertaking.

So, I was wondering whether there was a more straightforward way of doing this.

  • My final solution was to just directly download the css files of the website being scraped, and add references of these into the scraped html. – thisisnotworking4meplshelp Jun 22 '22 at 13:00
  • You could save the whole page as an MHTML file. This would take care of HTML tree, CSS and JS too. See https://stackoverflow.com/q/60352003/4720957 – user47 Jul 01 '22 at 17:54

0 Answers0