Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Open Local Files

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.

like image 628
Daniel Avatar asked Nov 29 '22 09:11

Daniel


1 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()
like image 97
Levi Noecker Avatar answered Dec 08 '22 08:12

Levi Noecker