1

So as the title suggests I am trying to accomplish a couple of things in Python 3.

Step 1 - I want to run a script that makes a folder with todays date. I am currently doing that like this:

import time
from selenium import webdriver
import os
from datetime import date

today = date.today()

os.mkdir('path/to/Screenshots' + ' ' + today.strftime('%m' + '-' + '%d' + '-' + '%Y'))

Step 2 Have webdriver open a chrome window to a link. I am currently doing that like this

driver = webdriver.Chrome('/path/to/chromedriver')
driver.get('http://www.google.com/');

Step 3 Where I am having my issue - I am trying to have webdriver take a screenshot and put it into the folder that I made in step 1. No I would think I do that like this:

driver.save_screenshot('path/to/Screenshots' + ' ' + today.strftime('%m' + '-' + '%d' + '-' + '%Y')/image.png')

This gives me the error SyntaxError: EOL while scanning string literal - please help me in understanding what I am doing wrong.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
DaveDoesDev
  • 53
  • 1
  • 3

2 Answers2

0

Try:

driver.save_screenshot('path/to/Screenshots' + ' ' + today.strftime('%m-%d-%Y') + '/image.png')

Tip:

If you're using python +3.6, f-Strings will help you avoiding this kind of errors by using a cleaner syntax, i.e.:

import time
td = time.strftime('%m-%d-%Y')
driver.save_screenshot(f'/path/to/Screenshots/{td}/image.png')
# /path/to/Screenshots/11-13-2019/image.png

Demo:

https://trinket.io/python3/c25a00b29a

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

To create a folder with today's date and save some screenshots that folder you can use the following Locator Strategy based solution:

  • Code Block:

    import os
    from datetime import date
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    datetoday = date.today()
    os.mkdir(str(datetoday))
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("http://www.google.com")
    for i in range(1,5):
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("DaveDoesDev")
        driver.save_screenshot("{}/image{}.png".format(datetoday, i))
    
  • Output:

    • This program will create a subdirectory with today's date.
    • All the screenshots will be save with different names.
  • Browser Snapshot:

date_directory

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352