How to deal with date picker in selenium using python? - python

I'm trying to select a range of dates from a date picker like this using selenium in python
Ex: Select date range today()+1 to today()+10 days
Currently, I am able to open this page click on the calendar and then click on the custom date. After which my next step would be to select the date range and click apply.
Below is the code:
`
from selenium import webdriver
from datetime import date, timedelta
browser = webdriver.Chrome('*driver path*')
browser.get('https://demo.improvely.com/reports/webshop/ads?imt=1&utm_campaign=Date+Range+Picker&utm_source=Site+Ads&utm_medium=Banner&utm_content=Blog+Demo+Image')
date_picker = browser.find_element_by_id('daterange').click()
browser.find_element_by_xpath("/html/body/div[7]/div[1]/ul/li[7]").click()
today = date.today()
from_date = today + timedelta(days=1)
to_date = today + timedelta(days=10)
`
Please help me out with a solution to get this done.
Many thanks in advance

Below xpath will give you all the dates in current month
elements = driverInstance.find_elements_by_xpath("//div[#class='calendar left']/descendant::*[#class='table-condensed']/child::*[2]/descendant::*")
Traverse through all the dates and select desired one.
for dates in elements:
Selectdate = dates.get_attribute("innerText")
if Selectdate== from_date:
dates.click()
Hope this helps.

Related

How to print a specific string on a specific date using openpyxl

I am trying to create a rolling rota using openpyxl. Some staff members work a 4 on 4 off rolling shift and I am trying to print "N/A" on the dates they are not working.
So far I have the following code:
from datetime import date
today = date.today()
I have tried the following code:
if today == "2022-02-21":
sheet["D13"] = "N/A"
This does not seem to print "N/A" in my desired cell.
I hope my query is not too confusing. Any help will be appreciated.
You have to convert the string to date. probably you will have to iterate over that column and convert it to date, or you can go with the solution offered by mwo.
from datetime import datetime
your_date = '2022-02-21'
date_obj = datetime.strptime(your_date, '%Y-%m-%d')

How to get correct date instead of system date in python

I'm working on a project, there's one problem, I need to fetch a correct date. and I can also fetch date from web --> Python Getting date online? , but the date will not be correct for every single person, Like if someone is living in USA so that's a problem, So how can I implement to fetch correct time for everyone.
THANKS
import requests
import datetime
import time
r = requests.get('http://just-the-time.appspot.com/')
utc_current_time = datetime.datetime.strptime(r.content.decode().strip(), '%Y-%m-%d %H:%M:%S')
print(f'utc_current_time: {utc_current_time}')
utc_offset_in_seconds = time.timezone
if(utc_offset_in_seconds > 0):
local_time = utc_current_time - datetime.timedelta(seconds=abs(time.timezone))
else:
local_time = utc_current_time + datetime.timedelta(seconds=abs(time.timezone))
print(f'local_time: {local_time}')
import pytz
from datetime import datetime
tz = pytz.timezone('America/Tijuana')
now = datetime.now(tz)
Or you can use arrow https://arrow.readthedocs.io/en/stable/
import arrow
arrow.now('America/New_York')

how to change date and month?

this is a piece of code where I want to change the date and month in the link. I don't want to use the date-time module because I will be scraping and storing some information from the website for each day and for each month I will be saving the data in CSV so after every month I want to create a new CSV file
also if there would be a for loop for a month I want to use it in naming the CSV file while saving it
from selenium import webdriver
import time
path="C:\\Users\\Nihal\\Downloads\\chromedriver_win32\\chromedriver.exe"
import numpy
driver = webdriver.Chrome(path)
driver.get('https://www.wunderground.com/history/daily/in/mangalore/VOML/date/2013-4-14')
Plug the dates as tuples and use f-string to format them in URL:
from selenium import webdriver
import time
path="C:\\Users\\Nihal\\Downloads\\chromedriver_win32\\chromedriver.exe"
import numpy
driver = webdriver.Chrome(path)
dates = [(2013, 4, 14)]
for x in dates:
#https://www.wunderground.com/history/daily/in/mangalore/VOML/date/2013-4-14
driver.get(f'https://www.wunderground.com/history/daily/in/mangalore/VOML/date/{x[0]}-{x[1]}-{x[2]}')
I am not used to selenium, so I can't help you with the parsing that you do with it, but I am pretty sure that you can use the datetime module to iterate on every day of a month and on every month of the year. Here is an example of how you can iterate on this and generate the url that you need:
from datetime import timedelta, date
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
start_date = date(2020, 8, 1)
end_date = date(2020, 8, 31)
days_to_scrape = []
for single_date in daterange(start_date, end_date):
days_to_scrape.append(f'https://www.wunderground.com/history/daily/in/mangalore/VOML/date/{single_date.strftime("%Y-%m-%d")}')
#Iteration with driver.get
If I understand, you will have no choice but to send as many requests as the number of days from which you want the data. You can then iterate on the items of the list with your scraping command. If there is another reason why you think datetime module can't do what you need it to do, please, explain it.
L.R.
P.S.
Thanks to vinzee who helped me to understand that kind of iteration with his answer: Iterating through a range of dates in Python

how to print previous 30 days from a date in python and the same in Tkinter

I am using Tkinter to build a Corona Travel History Questionnaire.
The front end has options to select a particular date - " D-Day Cronoa Confirmed".
Once the D-day is confirmed, the program is supposed to give fields for entering data on travel history.
I am using tkcalendar module in Python.
Problem: How to print the previous 30 days from a given date in python?
Question: Can we do anything in tkcalendar to print the last 30 days from a selected date in the window itself?
Using the datetime module you can find the last thirty days from any given date.
For example, the code:
first_date = datetime.datetime.now() - datetime.timedelta(30)
will give you the start period of this 30-day window. From there you simply need to fill in the gaps.
datetime and timedelta are made for this.
from datetime import datetime, timedelta
start = datetime(2020, 1, 1)
for day in range(1, 31):
print(start-timedelta(days=day))

Compare WebElement with Timedelta

i have a HTML Table with 5 columns. In the third column are links and in the fifth column is a Date.
Now i want to write a code which check if the date is within the next 4 weeks and if yes, then click on the link
This is what i have so far:
# Set the Date
start = time.strftime('%d-%m-%Y')
now = datetime.datetime.strptime(start, '%d-%m-%Y')
date_in_four_weeks = now + datetime.timedelta(days=28)
project_time = date_in_four_weeks - now
# Check the date and click on link
for i in range(project_time.days + 1):
print(now + timedelta(days=i))
time = driver.find_element_by_css_selector('css_selector')
if time <= project_time:
linkList = driver.find_elements_by_css_selector("css_selector")
for i in range(0,len(linkList)):
links = driver.find_elements_by_partial_link_text('SOLI')
links[i].click()
driver.get_screenshot_as_file("test.png")
else:
print "No Project found"
If i run the code i get the error:
TypeError: can't compare datetime.timedelta to WebElement
Now i want to ask if there is any way how i can fix my problem?
Thanks :)
There's a few issues you have to address.
Firstly, you're comparing to a WebElement object, as the error helpfully points out. This includes the tags and such of the HTML element you're referencing. You first want to extract the text.
Then, you need to parse this text to convert it into a Python datetime or date object. A time object won't do because it only stores time, and not date. Since I don't know what format your HTML date is in, I'll just point you to the docs so you can see how the types work and have some idea of how to parse your data.
Finally, you'd still get an error because of trying to compare a timedelta object to a date or datetime object. A timedelta is a period of time, it doesn't relate to a specific date.
You could fix this by replacing
if time <= project_time:
With the same as from your print function:
if time <= now + timedelta(days=i)

Categories

Resources