Selenium Open Local Files

Question:

I am attempting to use a Firefox/Selenium instance as a rudimentary slideshow for images. The idea is that I will open a webdriver and driver.get() files from a local directory.

When I run the following, I receive an error:
selenium.common.exceptions.WebDriverException: Message: Tried to run command without establishing a connection

My assumption is that selenium is attempting to test the next driver.get() request and is not allowing a local, non web-connected, connection is there a way to bypass this behavior? My code example appears below:

from selenium import webdriver
import time
from os import listdir
from selenium.common.exceptions import WebDriverException

driver = webdriver.Firefox()

image_source = '/home/pi/Desktop/slideshow/photo_frames/daniel/images/'

for file in listdir(image_source):
    if file.endswith('jpg'):
        file_name = image_source + file
        driver.get(file_name)
        time.sleep(5)

UPDATE:
I should add that the same basic script structure works for websites – I can loop through several websites without any errors.

Asked By: Daniel

||

Answers:

I think you are just need to add file:// to the filename. This works for me:

from selenium import webdriver
import time
from os import listdir
from selenium.common.exceptions import WebDriverException

def main():
    image_source = '/home/pi/Desktop/slideshow/photo_frames/daniel/images/'

    driver = webdriver.Firefox()

    try:
        for file in listdir(image_source):
            if file.endswith('jpg'):
                file_name = 'file://' + image_source + file
                driver.get(file_name)
                time.sleep(5)
    finally:
        driver.quit()

if __name__ == "__main__":
    main()
Answered By: Levi Noecker

If you came in here wanting Selenium to serve your local html file, as I did, the above-mentioned accepted answer needs a tiny modification for it to work, as noticed by Niklas Rosencrantz.

For Selenium to serve local html in your browser, and assuming the file is in your current working directory, try this (I’m on a Windows, using Selenium 3.141.0 and Python 3.7 – if it matters to you):

from selenium import webdriver
import os

browser = webdriver.Firefox()
html_file = os.getcwd() + "//" + "relative//path//to//file.html"
browser.get("file:///" + html_file)
Answered By: adriaanbd

This can also be done with Pathlib

from selenium import webdriver
from pathlib import Path

browser = webdriver.Firefox()
html_file = Path.cwd() / "relative//path//to//file.html"
browser.get(html_file.as_uri())

If you are new to pathlib then the / syntax can look a bit odd, but it’s very easy to use, here is a good tutorial https://realpython.com/python-pathlib/

Answered By: Dan Heaford

An edit from Dan
Heaford’s
anwser to open local html file:

Html: <body><h1>I'm an h1</h1></body>

from selenium.webdriver.common.by import By
from selenium import webdriver
from pathlib import Path
chrome_driver_path = "#'** Dir where you saved your 
                       chromedriver.exe**'/Users/ASUS/chromedriver"

browser = webdriver.Chrome(executable_path=chrome_driver_path)
html_file = Path.cwd() / "#html file Dir.html"
browser.get(html_file.as_uri())

heading1 = browser.find_element(By.TAG_NAME, 'h1')
print(heading1.text)
result in console - "I'm an h1"
Answered By: Fabian Joseph
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.