14

I am trying to run Selenium on a local HTML string but can't seem to find any documentation on how to do so. I retrieve HTML source from an e-mail API, so Selenium won't be able to parse it directly. Is there anyway to alter the following so that it would read the HTML string below:

Python Code for remote access:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_class_name("q")

Local HTML Code:

s = "<body>
        <p>This is a test</p>
        <p class="q">This is a second test</p>
     </body>"
2
  • should that not be .find_element_by_class_name ? is that the underlying problem or is this just a typo Commented Dec 29, 2015 at 20:38
  • What is the expected output or why is it now working? Commented Dec 29, 2015 at 21:41

4 Answers 4

22

If you don't want to create a file or load a URL before being able to replace the content of the page, you can always leverage the Data URLs feature, which supports HTML, CSS and JavaScript:

from selenium import webdriver

driver = webdriver.Chrome()
html_content = """
<html>
     <head></head>
     <body>
         <div>
             Hello World =)
         </div>
     </body>
</html>
"""

driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))
Sign up to request clarification or add additional context in comments.

2 Comments

This is limited to 65535 characters.
Yes and no, it actually depends on the browser that you are using. CF stackoverflow.com/a/41755526/5061294.
16

If I understand the question correctly, I can imagine 2 ways to do this:

  1. Save HTML code as file, and load it as url file:///file/location. The problem with that is that location of file and how file is loaded by a browser may differ for various OSs / browsers. But implementation is very simple on the other hand.
  2. Another option is to inject your code onto some page, and then work with it as a regular dynamic HTML. I think this is more reliable, but also more work. This question has a good example.

2 Comments

I think I'll go with the injection. Thanks.
I found below works for me location = "file:///Users/your_username/github/login_automation/landing.html" driver.get(location) print (driver.page_source)
6

Here was my solution for doing basic generated tests without having to make lots of temporary local files.

import json
from selenium import webdriver
driver = webdriver.PhantomJS()  # or your browser of choice

html = '''<div>Some HTML</div>'''
driver.execute_script("document.write('{}')".format(json.dumps(html)))
# your tests

3 Comments

Not sure why this was downvoted, but seems to do the trick for me.
Sad, but throws a JavascriptException: SecurityError: The operation is insecure.
re.escape is more appropriate than json.dumps to turn the special characters of a string into escaped characters. The latter won't escape single quotes, for example, and this breaks the string within document.write()
-1

If I am reading correctly you are simply trying to get text from an element. If that is the case then the following bit should fit your needs:

    elem = driver.find_element_by_class_name("q").text
    print elem

Assuming "q" is the element you need.

1 Comment

I'm trying to parse a local string. So rather that feed a url to "driver.get", I would want to pass the html string to it.

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.