TestProject Forum
Powered by leading experts in the test automation community

Selenium in python accessing HTML source

Can I get the HTML source in a variable using selenium module with Python?

I’m trying to do something like this:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get(raw_input("url:"))
if "text" in html_source:
    # logic
else:
    # logic

Is this possible? How can I access the HTML source?

You can if you call the page_source property:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get(raw_input("url: "))
html_source = browser.page_source
if (condition) in html_source:
    # logic
else:
    # logic