Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load up PhantomJS with a Python string

I'm using PhantomJS as a Selenium webdriver, and I have an in-memory string of a webpage.

How can I load the string into PhantomJS akin to what would happen if I did driver.get()?

I've looked all over but can't find anything like this short of writing a file to disk and GETing that. But that feels a little crazy.

like image 905
mlissner Avatar asked Feb 03 '26 05:02

mlissner


1 Answers

Did you mean something like this by Data URI scheme?

demo_page = '''
    data:text/html,
    <!doctype html>

    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Test "How to load up PhantomJS with a Python string"</title>
    </head>

    <body>
        <a href="http://stackoverflow.com/q/24834838/1177636">How to load up PhantomJS with a Python string</a>
    </body>
    </html>
'''

driver = webdriver.PhantomJS()
driver.get(demo_page)

driver.save_screenshot('screenshot.png')
driver.quit()
like image 94
Yi Zeng Avatar answered Feb 04 '26 18:02

Yi Zeng