Python Selenium accessing HTML source


Question

How can I get the HTML source in a variable using the Selenium module with Python?

I wanted to do something like this:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get(raw_input("Enter URL: "))
if "whatever" in html_source:
    # Do something
else:
    # Do something else

How can I do this? I don't know how to access the HTML source.

1
85
12/31/2018 9:32:30 AM

You need to call the page_source property. See below.

from selenium import webdriver
browser = webdriver.Firefox()
browser.get(raw_input("Enter URL: "))
html_source = browser.page_source
if "whatever" in html_source:
    # do something
else:
    # do something else
167
10/23/2011 3:08:06 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon