0

I'm trying to automate a task that I have to do every week using Python and Selenium.

I go to a website and if there is any new files I download them, rename them using the date they came in and who they went to, and then place them in a folder on the shared network server.

The website provides the date in which the file comes in through a clickable link.

Using find elements by xpath, and what I assume are parameters, starts with and contains, I've been able to search through all the links with the date and time.

receivedTime = browser.find_elements_by_xpath('//*[starts-with(@id, 
"anchor") and contains(@id, "_0")]')
for time in receivedTime:
 print(time.text)

The output looks like this for example, "11/2/2018, 8:00:50 AM".

I would like to format that text to say "2018-11-02", how would I go about doing that?

It's my understanding that the variable time is just an object of the Current Xpath and .text is just a property of that object. Is my understanding correct?

Thank you.

ANSWER:

receivedTime = browser.find_elements_by_xpath('//*[starts-with(@id, 
"anchor") and contains(@id, "_0")]')
for time in receivedTime:
 date = str(time.text).split(',')
 dateTime = datetime.strptime(date[0], '%m/%d/%Y').strftime('%Y-%m-%d-')
 print(dateTime)

2 Answers 2

2

You should use the package datetime (import datetime)
The time variable is a string so you have to convert it into datetime and change the format like this :

date = str(time.text).split(',')
datetime.datetime.strptime(date[0], '%m/%d/%Y').strftime('%Y-%m-%d')
Sign up to request clarification or add additional context in comments.

5 Comments

I'm already using the package datetime to compare dates, so this is nice.
If I understand this correctly. You set the date as a variable to equal the string of time.text then used the command split to split the string before and after the , From there, you use the datetime package and the string to datetime function to convert the string to a datetime. (Best guess) - date[0] is setting the variable date to only what is in front of the comma and the 0 removes what is after it. Also formatting the datetime in the process Then using strftime to convert datetime back to string in the wanted format. Is that correct?
receivedTime = browser.find_elements_by_xpath('//*[starts-with(@id, "anchor") and contains(@id, "_0")]') for time in receivedTime: date = str(time.text).split(',') dateTime = datetime.strptime(date[0], '%m/%d/%Y').strftime('%Y-%m-%d-') print(dateTime)
Yes that's correct, I used split() to select just 11/2/2018 because this is what need to be formatted.
Thank you for answering. I found my answer using the code and logic you provided with some alterations. You can find the code after the edit I made to my question.
0

You can also use a regular expression to extract the numbers and reformat the date:

import re
text = "11/2/2018, 8:00:50 AM"
date_tuple = re.match("(\d+)\/(\d+)\/(\d+)", text).groups()
file_name = "%d-%02d-%02d" % (int(date_tuple[2]), int(date_tuple[0]), int(date_tuple[1]))

Result: "2018-11-02"

1 Comment

Never have used the package re before. Would have to put text as whatever the xpath currently is as it changes. Not sure what the d+ are in date_tuple, best guess is a placeholder. Not sure what the %02 in file_name stand for. Everything else I understand on certain level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.