135

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("http://example.com")
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.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
user1008791
  • 1,379
  • 2
  • 10
  • 5

8 Answers8

252

You need to access the page_source property:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("http://example.com")

html_source = browser.page_source
if "whatever" in html_source:
    # do something
else:
    # do something else
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
18
from bs4 import BeautifulSoup
from selenium import webdriver

driver = 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...

Mobin Al Hassan
  • 954
  • 11
  • 22
8

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 webdriver
driver = 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:
Dhiraj
  • 427
  • 8
  • 20
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Nov 19 '18 at 17:39
5

With Selenium2Library you can use get_source()

import Selenium2Library
s = Selenium2Library.Selenium2Library()
s.open_browser("localhost:7080", "firefox")
source = s.get_source()
Milanka
  • 1,742
  • 19
  • 15
  • 12
    Can I set a delay and get the latest source? There are dynamic contents loaded using javascript. – CodeGuru Oct 17 '13 at 23:36
3

By using the page source you will get the whole HTML code.
So first decide the block of code or tag in which you require to retrieve the data or to click the element..

options = driver.find_elements_by_name_("XXX")
for option in options:
    if option.text == "XXXXXX":
        print(option.text)
        option.click()

You can find the elements by name, XPath, id, link and CSS path.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
2

You can simply use the WebDriver object, and access to the page source code via its @property field page_source...

Try this code snippet :-)

from selenium import webdriver
driver = webdriver.Firefox('path/to/executable')
driver.get('https://some-domain.com')
source = driver.page_source
if 'stuff' in source:
    print('found...')
else:
    print('not in source...')
SysMurff
  • 126
  • 2
  • 14
1

To answer your question about getting the URL to use for urllib, just execute this JavaScript code:

url = browser.execute_script("return window.location;")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bob Evans
  • 616
  • 6
  • 18
-7

I'd recommend getting the source with urllib and, if you're going to parse, use something like Beautiful Soup.

import urllib

url = urllib.urlopen("http://example.com") # Open the URL.
content = url.readlines() # Read the source and save it to a variable.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Griffin
  • 644
  • 6
  • 18
  • Okay then do you know how I can get the URL within Selenium? I want to store the URL in a variable so I can access it with urllib. – user1008791 Oct 22 '11 at 19:07
  • @user1008791 Does it matter? You're apparently letting the user type it in anyway using raw_input, just do the same but with urllib. – Griffin Oct 22 '11 at 19:10
  • That was just to make an easy example, the URL will be changing a lot. – user1008791 Oct 22 '11 at 19:40
  • 8
    Selenium does many things that urllib doesn't (e.g. execution of JavaScript). – mpenkov Aug 28 '12 at 07:04
  • Using the urllib here is pointless, why? AutomatedTester has it correct, it is what I do for scanning through HTML source to make sure we don't push development environment code. – Dave Sep 24 '13 at 23:27