'Python/Selenium save an image from site to a specific folder instead of script folder

I have a code with sekenium that retrieves 100 different captcha images from zefoy.com, but the images get saved in the folder my script is in but I want to save them to a specific folder:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.maximize_window()
driver.get("https://zefoy.com")
folder = r'C:\Users\fkahd\OneDrive\Bureau\zefoy\captchas'

for i in range(100):
   with open('captcha('+str(i)+').png', 'wb') as file:
      captcha = driver.find_element(By.XPATH, '//*[@id="main"]/form/div/div/img')
      file.write(captcha.screenshot_as_png) #write to folder
   driver.refresh()

Thanks!



Solution 1:[1]

I figured out a way which is to add the folder path before naming the image:

with open(r'C:\Users\fkahd\OneDrive\Bureau\zefoy\captchas\captcha('+str(i)+').png', 'wb') as file:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xtekky