Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 99418

How to change the target directory for a screenshot using Selenium webdriver in Firefox or Chrome

$
0
0

I want to make a screenshot of a webpage and save it in a custom location using Selenium webdriver with Python. I tried saving the screenshot to a custom location using both Firefox and Chrome but it always saves the screenshot in the project dir. Here is my Firefox version:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.dir", 
'C:\\Users\\User\\WebstormProjects')
binary = FirefoxBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe")


def foxScreen():
    driver = webdriver.Firefox(firefox_binary=binary, 
    firefox_profile=profile)
    driver.get("http://google.com")
    driver.save_screenshot("foxScreen.png")
    driver.quit()


if __name__ == '__main__':
    foxScreen()

And here is my Chrome version:

from selenium import webdriver

options = webdriver.ChromeOptions()
prefs = {"download.default_directory": r'C:\\Users\\User\\WebstormProjects',
         "directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
chromedriver = 
"C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe"


def chromeScreen():
    driver = webdriver.Chrome(chrome_options=options, 
                              executable_path=chromedriver)
    driver.get("http://google.com")
    driver.save_screenshot("chromeScreen.png")
    driver.quit()


if __name__ == '__main__':
    chromeScreen()

I have tried different notations for the location I want the screenshot saved to but that does not seem to help. What should I change so it does not save the screenshot to the project directory but to a given custom location?


Viewing all articles
Browse latest Browse all 99418

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>