0

How can one get all the text of a Webdriver instance?

To get the text of a Webelement is straightforward, as explained for instance here:

element1.text

or

element1.get_attribute('innerHTML')

However, to get it from the Webdriver element I did not find similar solution. I just figured out the following two-step approach:

driver.get(url)
driver.find_element_by_tag_name('body').text

but it seems to me that there has to be a better way...

Community
  • 1
  • 1
J0ANMM
  • 7,849
  • 10
  • 56
  • 90

1 Answers1

2

You may probably try:

driver.get(url)
page_source = driver.page_source  # This gives you a 'str' containing the html source
print(page_source)

See also Python Selenium accessing HTML source.

If you need to parse the page_source, BeautifulSoup4 is also a useful tool.

Community
  • 1
  • 1
mikeqfu
  • 329
  • 2
  • 10