Python Selenium accessing HTML source Python Selenium accessing HTML source python python

Python Selenium accessing HTML source


You need to access the page_source property:

from selenium import webdriverbrowser = webdriver.Firefox()browser.get("http://example.com")html_source = browser.page_sourceif "whatever" in html_source:    # do somethingelse:    # do something else


driver.page_source will help you get the page source code. You can check if the text is present in the page source or not.

from selenium import webdriverdriver = webdriver.Firefox()driver.get("some url")if "your text here" in driver.page_source:    print('Found it!')else:    print('Did not find it.')

If you want to store the page source in a variable, add below line after driver.get:

var_pgsource=driver.page_source

and change the if condition to:

if "your text here" in var_pgsource:


from bs4 import BeautifulSoupfrom selenium import webdriverdriver = webdriver.Chrome()html_source_code = driver.execute_script("return document.body.innerHTML;")html_soup: BeautifulSoup = BeautifulSoup(html_source_code, 'html.parser')

Now you can apply BeautifulSoup function to extract data...