sameer.joshi
sameer.joshi

Reputation: 81

datetime in selenium python throwing error

Below code gives error as 'friday' no defined. If anything is missing, please help me with the same. Also if I interchange the position of 'friday' and 'prev_day' variables with if else, i get the error message with 'prev_day' not defined.

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import datetime


d = datetime.date.today()

if d.weekday() == 0:
    tdelta = datetime.timedelta(days=3)
    friday = d - tdelta


elif d.weekday() in range(1,5):
    tdelta1 = datetime.timedelta(days=1)
    prev_day = d - tdelta1


class ClassName(unittest.TestCase):

    def setUp(self):

        self.driver = webdriver.Chrome(executable_path="C:\\Users\sameerj\IdeaProjects\chromedriver.exe")


    def test_as_on_date(self):

        driver = self.driver
        driver.maximize_window()
        driver.get("website")
        login = driver.find_element_by_id("Email")
        login.send_keys("email")
        password = driver.find_element_by_id("Password")
        password.send_keys("password")
        password.send_keys(Keys.ENTER)
        driver.find_element_by_id("menu_name").click()
        driver.find_element_by_partial_link_text("page name").click()
        date = driver.find_element_by_id("lblAsOn").text
        new = datetime.datetime.strptime(date,'%m/%d/%Y')
        data_date = new.date()

        if data_date == friday:
            print("Data as on", friday, "for page name")
        elif data_date == prev_day:
            print("Data as on", prev_day, "for page name")
        else:
            print("Data update required.")
        driver.close()


if __name__ == '__main__':
    unittest.main()

Upvotes: 2

Views: 335

Answers (3)

py_dude
py_dude

Reputation: 832

You better write:

d = datetime.date.today()

if d.weekday() == 0:
    tdelta = datetime.timedelta(days=3)
    prev_day = None
    friday = d - tdelta
elif d.weekday() in range(1, 5):
    tdelta1 = datetime.timedelta(days=1)
    prev_day = d - tdelta1
    friday = None

And my suggestion for you is to use Python IDEs, they highlight such errors.

Also I believe this checks could be done using the calendar module

Upvotes: 0

Nihal
Nihal

Reputation: 5334

Its a common type of mistake

let me break it down your mistake

if d.weekday() == 0:
    tdelta = datetime.timedelta(days=3)
    friday = d - tdelta


elif d.weekday() in range(1,5):
    tdelta1 = datetime.timedelta(days=1)
    prev_day = d - tdelta1

if we execute your program

if d.weekday() == 0 holds false

then it will go to

elif d.weekday() in range(1,5):

but your friday = d - tdelta is in if condition. that's why its shows error

to solve that you must define friday outside if condition and reassign value in your if condition

you can solve it like this

friday = None
prev_day = None
d = datetime.date.today()

if d.weekday() == 0:
    tdelta = datetime.timedelta(days=3)
    friday = d - tdelta


elif d.weekday() in range(1,5):
    tdelta1 = datetime.timedelta(days=1)
    prev_day = d - tdelta1

Upvotes: 2

Rupesh Goud
Rupesh Goud

Reputation: 131

new = datetime.datetime.strptime(date,'%m/%d/%Y')
data_date = new.strftime("%A")

This will give you the name of the day (Eg: Friday), then you can compare as you are doing,

if data_date == "Friday":
        print("Data as on",data_date , "for page name")

And for previous days comparison, you have to do,

tdelta1 = datetime.timedelta(days=1) prev_day = (new - tdelta1).strftime("%A")

before doing,

elif data_date == prev_day:
        print("Data as on", prev_day, "for page name")

When you replaced Friday with prev_day, you are getting the error because of python interpreter is finding that variable first which is not defined.

Upvotes: 0

Related Questions