7

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)

As always, any help would be greatly appreciated.

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

Daniel
  • 641
  • 2
  • 6
  • 18

3 Answers3

13

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()
Levi Noecker
  • 2,832
  • 1
  • 14
  • 28
  • Thank you - this does work. I want to believe I tried adding 'file://' to the beginning of the address, but your example definitely functions in the desired way. – Daniel Mar 24 '17 at 15:19
  • 4
    The above won't work for local html. The code gets stuck if I try this with local html. – Niklas Rosencrantz Sep 21 '17 at 08:51
9

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)
adriaanbd
  • 287
  • 3
  • 10
7

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/

Dan Heaford
  • 411
  • 3
  • 10