141

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.

1
  • 3
    Write following line before if condition: html_source = browser.page_source Commented Oct 23, 2014 at 13:21

9 Answers 9

267

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
Sign up to request clarification or add additional context in comments.

3 Comments

Best answer so far! The most immediate and clear way to do this, much more compact that the other, still valid, alternative (find_element_by_xpath("//*").get_attribute("outerHTML")(
What if we need to get page source after all the javascript executes.?
Works only if the page has completely loaded. If the page loads indefinitely this property doesn't work.
19
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...

Comments

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:

1 Comment

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.
5

With Selenium2Library you can use get_source()

import Selenium2Library
s = Selenium2Library.Selenium2Library()
s.open_browser("localhost:7080", "firefox")
source = s.get_source()

1 Comment

Can I set a delay and get the latest source? There are dynamic contents loaded using javascript.
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.

Comments

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;")

Comments

1

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...')

1 Comment

how does this answer differs from stackoverflow.com/a/7866938/2231972 ?
1

Complete code:

from selenium import webdriver

# Initialize the WebDriver
driver = webdriver.Chrome()  # Use the appropriate WebDriver for your browser

# Navigate to the desired URL
driver.get("https://www.example.com/")

# Access the page's HTML source
html_source = driver.page_source

if "whatever" in html_source:
   # do something
else:
   # do something else

# if you want to display complete source code.
print(html_source)

# Close the WebDriver
driver.quit()

Comments

-8

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.

5 Comments

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 Does it matter? You're apparently letting the user type it in anyway using raw_input, just do the same but with urllib.
That was just to make an easy example, the URL will be changing a lot.
Selenium does many things that urllib doesn't (e.g. execution of JavaScript).
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.