POST
|
My error message is not displaying in my code. Just testing how to use Selenium on Python. #import what I need
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
import time
#set my driver object
driver = webdriver.Chrome(ChromeDriverManager().install())
#go to facebook.com
driver.get('https://facebook.com/')
#make browser full screen
driver.maximize_window()
#type the email in the email field
email_field = driver.find_element_by_id('email')
email_field.send_keys("testemail123@gmail.com")
time.sleep(3)
#type the password in the password field
pss_field = driver.find_element_by_id('pass')
pss_field.send_keys("testpassword")
time.sleep(3)
#click the login button
#login_button = driver.find_element_by_xpath('//*[@id="u_0_q"]')
#login_button = driver.findElement(By.id("btnLogon"))
#login_button = driver.find_element_by_id('btnLogon')
#login_button = driver.find_element(By.ID, 'btnLogon')
login_button = WebDriverWait(driver, 30).until(lambda driver: driver.find_element_by_id('u_0_b'))
login_button.click()
#find all the text on the screen
body = WebDriverWait(driver, 30).until(lambda driver: driver.find_element_by_xpath('/html/body'))
#body = driver.find_element_by_tag_name('body')
all_text = body.text
#verify the error message is displayed
if "Sorry, something went wrong." not in all_text:
raise BaseException("The 'Sorry, something went wrong.' text is not found.")
else:
print("Test Passed")
time.sleep(20)
driver.quit()
... View more
05-31-2020
12:32 PM
|
0
|
1
|
860
|
POST
|
Thank you for further information and coding about this, very beneficial!
... View more
01-04-2020
11:28 AM
|
0
|
0
|
484
|
POST
|
Hello Blake, May you explain a little bit more about the code, specifically this part of the code: indexHTML = requests.get("https://www.gosolarcalifornia.ca.gov/equipment/documents/").text
fileName = "Grid_Support_Inverter_List_Full_Data.xlsm"
fileNameHTML = '<td><a href="{0}">{0}</a></td>'.format(fileName)
fileNameHTMLIndex = indexHTML.find(fileNameHTML)
if fileNameHTMLIndex != -1:
dateStartIndex = indexHTML.find(">", fileNameHTMLIndex+len(fileNameHTML)) + 1
dateEndIndex = indexHTML.find("<", dateStartIndex)
webFileDate = datetime.datetime.strptime(indexHTML[dateStartIndex:dateEndIndex].strip(), "%Y-%m-%d %H:%M")
else:
raise Exception("Unable to find fileName {}".format(fileName)) Line 4 will return a -1 if fileNameHTML is not found in indexHTML. So, line 5 checks for the presence of a <td> tag with the filename. In the event it is found (the value is not -1), it then checks for the first ">" and the first "<" in indexHTML on lines 6 and 7. Using the returned indexes, it slices indexHTML, strips the leading and trailing white space, and formats the text into a datetime object.
... View more
01-02-2020
12:02 PM
|
0
|
2
|
917
|
POST
|
Sweet worked. Can I basically use that link to change the formatting of the date to any combination of date formats? For example, change it to 12-30-2019 and etc.? Also, how can I mark this discussion/post as done/correct?
... View more
01-02-2020
09:33 AM
|
0
|
1
|
484
|
POST
|
Hello Randy, Thanks so much for the help, been stuck on this for a week. One small error, how can I change 19-12-30 to 2019-12-30? Code In Case: #!/usr/bin/env python
import requests
import os.path
import sys
import shutil
import pandas as pd
from datetime import datetime
import time
def getFile():
url = "https://www.gosolarcalifornia.ca.gov/equipment/documents/Grid_Support_Inverter_List_Full_Data.xlsm"
content = requests.get(url)
date = datetime.date(datetime.now())
directory = "C:\\Users\\slee5\\Desktop\\Folder"
archivedDirectory = "C:\\Users\\slee5\\Desktop\\Folder\\ArchivedFolder"
#date = datetime.datetime.fromtimestamp(os.path.getmtime(archivedDirectory)))
fileName = "Grid_Support_Inverter_List_Full_Data"
extension = ".csv"
path = fileName + extension
print ("Files will be saved in the following directory: "+directory+"\n")
print ("Old Files with the same name will be archived in the following directory: "+archivedDirectory+"\n")
print ("NOTICE: File will be converted to .csv file...")
if os.path.isdir(directory):
#Check if the Archived Folder Exists, if it does not create it
if(os.path.isdir(archivedDirectory)):
pass
else:
os.mkdir(archivedDirectory)
print ("SUCCESS: Directory was found.")
counter = 0
if (os.path.exists(directory+"\\"+fileName+extension)):
counter += 1
while (os.path.exists(archivedDirectory+"\\"+fileName+"_%s_%s"%(str(date), str(counter))+extension)):
counter += 1
if counter > 0:
print ("File: "+fileName+" already exists in directory,moving old file to Archived Folder. ")
shutil.move(directory+"\\"+fileName+extension, archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension)
path1 = archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension
date1 = datetime.fromtimestamp(os.path.getmtime(path1))
#date1 = str(date1) # delete or comment out - this line is not needed and is causing the error
date1 = date1.strftime('_%y-%m-%d') # this converts datetime object to a formatted string - add and underscore to format
os.rename(path1,archivedDirectory+"\\"+fileName+date1+extension) # previous line formats date1 to text
with open(os.path.join(directory, path), 'wb') as f:
f.write(content.content)
deleteRows(directory, path)
print("Done.")
else:
print("ERROR: Directory is not found.")
sys.exit()
def deleteRows(directory, path):
newFile = pd.read_excel(os.path.join(directory,path))
newFile = newFile.drop(range(14), axis = 0)
newFile.to_csv(os.path.join(directory, path), encoding='utf8', header=False, index=False)
def main():
getFile()
if __name__ == "__main__":
main()
... View more
01-01-2020
09:16 PM
|
0
|
3
|
1866
|
POST
|
Hello Randy, I get an error from doing that. Been stuck on this issue for a long time, used every single online resource out there and still couldn't fix the issue. The error only occurs if I already have a file in the folder directory. Code: #!/usr/bin/env python
import requests
import os.path
import sys
import shutil
import pandas as pd
from datetime import datetime
import time
def getFile():
url = "https://www.gosolarcalifornia.ca.gov/equipment/documents/Grid_Support_Inverter_List_Full_Data.xlsm"
content = requests.get(url)
date = datetime.date(datetime.now())
directory = "C:\\Users\\slee5\\Desktop\\Folder"
archivedDirectory = "C:\\Users\\slee5\\Desktop\\Folder\\ArchivedFolder"
#date = datetime.datetime.fromtimestamp(os.path.getmtime(archivedDirectory)))
fileName = "Grid_Support_Inverter_List_Full_Data"
extension = ".csv"
path = fileName + extension
print ("Files will be saved in the following directory: "+directory+"\n")
print ("Old Files with the same name will be archived in the following directory: "+archivedDirectory+"\n")
print ("NOTICE: File will be converted to .csv file...")
if os.path.isdir(directory):
#Check if the Archived Folder Exists, if it does not create it
if(os.path.isdir(archivedDirectory)):
pass
else:
os.mkdir(archivedDirectory)
print ("SUCCESS: Directory was found.")
counter = 0
if (os.path.exists(directory+"\\"+fileName+extension)):
counter += 1
while (os.path.exists(archivedDirectory+"\\"+fileName+"_%s_%s"%(str(date), str(counter))+extension)):
counter += 1
if counter > 0:
print ("File: "+fileName+" already exists in directory,moving old file to Archived Folder. ")
shutil.move(directory+"\\"+fileName+extension, archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension)
path1 = archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension
date1 = datetime.fromtimestamp(os.path.getmtime(path1))
date1 = str(date1)
date1 = date1.strftime('%y-%m-%d')
os.rename(path1,archivedDirectory+"\\"+fileName+"_%s"%(str(date1).replace(":"," "))+extension)
with open(os.path.join(directory, path), 'wb') as f:
f.write(content.content)
deleteRows(directory, path)
print("Done.")
else:
print("ERROR: Directory is not found.")
sys.exit()
def deleteRows(directory, path):
newFile = pd.read_excel(os.path.join(directory,path))
newFile = newFile.drop(range(14), axis = 0)
newFile.to_csv(os.path.join(directory, path), encoding='utf8', header=False, index=False)
def main():
getFile()
if __name__ == "__main__":
main()
Here is some background information for the script:
... View more
01-01-2020
03:52 PM
|
0
|
5
|
1866
|
POST
|
Update: How to I fix the fille name of the file in the archived folder to show file name + modified date? Modified date: 2019-12-30 Disregard the first file. I fixed the code so both files don't show in archived folder. I renamed path to path1 since path is already used previously, so I made a new variable called path1.
... View more
12-31-2019
09:43 AM
|
0
|
0
|
1886
|
POST
|
I was able to get pass the syntax error but the expected result didn't show. Expected Result: Old file is supposed to be renamed Grid_Support_....._2019-12-30 New File is supposed to be named Grid_Support_.....with no date on it New file is supposed to be inside Folder directory but for some reason it moved to Archived Directory Old file is moved to the archived directory I have attached a copy of the excel file in case you wanted to test it on your computer. The date on it is 12-30-2019 so you can actually use it. If it was 12-31-2019 then the results that you wanted to see won't show. Code: #!/usr/bin/env python
import requests
import os.path
import sys
import shutil
import pandas as pd
from datetime import datetime
import time
def getFile():
url = "https://www.gosolarcalifornia.ca.gov/equipment/documents/Grid_Support_Inverter_List_Full_Data.xlsm"
content = requests.get(url)
date = datetime.date(datetime.now())
directory = "C:\\Users\\slee5\\Desktop\\Folder"
archivedDirectory = "C:\\Users\\slee5\\Desktop\\Folder\\ArchivedFolder"
#date = datetime.datetime.fromtimestamp(os.path.getmtime(archivedDirectory)))
fileName = "Grid_Support_Inverter_List_Full_Data"
extension = ".csv"
path = fileName + extension
print ("Files will be saved in the following directory: "+directory+"\n")
print ("Old Files with the same name will be archived in the following directory: "+archivedDirectory+"\n")
print ("NOTICE: File will be converted to .csv file...")
if os.path.isdir(directory):
#Check if the Archived Folder Exists, if it does not create it
if(os.path.isdir(archivedDirectory)):
pass
else:
os.mkdir(archivedDirectory)
print ("SUCCESS: Directory was found.")
counter = 0
if (os.path.exists(directory+"\\"+fileName+extension)):
counter += 1
while (os.path.exists(archivedDirectory+"\\"+fileName+"_%s_%s"%(str(date), str(counter))+extension)):
counter += 1
if counter > 0:
print ("File: "+fileName+" already exists in directory,moving old file to Archived Folder. ")
shutil.move(directory+"\\"+fileName+extension, archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension)
path = archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension
date1 = time.ctime(os.path.getmtime(path))
os.rename(path,archivedDirectory+"\\"+fileName+"_%s"%(str(date1).replace(":"," "))+extension)
with open(os.path.join(directory, path), 'wb') as f:
f.write(content.content)
deleteRows(directory, path)
print("Done.")
else:
print("ERROR: Directory is not found.")
sys.exit()
def deleteRows(directory, path):
newFile = pd.read_excel(os.path.join(directory,path))
newFile = newFile.drop(range(14), axis = 0)
newFile.to_csv(os.path.join(directory, path), encoding='utf8', header=False, index=False)
def main():
getFile()
if __name__ == "__main__":
main()
Actual Result:
... View more
12-31-2019
08:53 AM
|
0
|
2
|
1886
|
POST
|
Update: Get this error with those two additions from your hint. date1 = time.ctime(os.path.getmtime(path))
date1 = str(date1)
date1= date1.strftime('%d-%m-%y')
... View more
12-30-2019
10:28 AM
|
0
|
4
|
1886
|
POST
|
#!/usr/bin/env python
import requests
import os.path
import sys
import shutil
import pandas as pd
from datetime import datetime
import time
def getFile():
url = "https://www.gosolarcalifornia.ca.gov/equipment/documents/Grid_Support_Inverter_List_Full_Data.xlsm"
content = requests.get(url)
date = datetime.date(datetime.now())
directory = "C:\\Users\\slee5\\Desktop\\Folder"
archivedDirectory = "C:\\Users\\slee5\\Desktop\\Folder\\ArchivedFolder"
#date = datetime.datetime.fromtimestamp(os.path.getmtime(archivedDirectory)))
fileName = "Grid_Support_Inverter_List_Full_Data"
extension = ".csv"
path = fileName + extension
print ("Files will be saved in the following directory: "+directory+"\n")
print ("Old Files with the same name will be archived in the following directory: "+archivedDirectory+"\n")
print ("NOTICE: File will be converted to .csv file...")
if os.path.isdir(directory):
#Check if the Archived Folder Exists, if it does not create it
if(os.path.isdir(archivedDirectory)):
pass
else:
os.mkdir(archivedDirectory)
print ("SUCCESS: Directory was found.")
counter = 0
if (os.path.exists(directory+"\\"+fileName+extension)):
counter += 1
while (os.path.exists(archivedDirectory+"\\"+fileName+"_%s_%s"%(str(date), str(counter))+extension)):
counter += 1
if counter > 0:
print ("File: "+fileName+" already exists in directory,moving old file to Archived Folder. ")
shutil.move(directory+"\\"+fileName+extension, archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension)
path = archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension
date1 = time.ctime(os.path.getmtime(path))
os.rename(path,archivedDirectory+"\\"+fileName+"_%s"%(str(date1))+extension)
with open(os.path.join(directory, path), 'wb') as f:
f.write(content.content)
deleteRows(directory, path)
print("Done.")
else:
print("ERROR: Directory is not found.")
sys.exit()
def deleteRows(directory, path):
newFile = pd.read_excel(os.path.join(directory,path))
newFile = newFile.drop(range(14), axis = 0)
newFile.to_csv(os.path.join(directory, path), encoding='utf8', header=False, index=False)
def main():
getFile()
if __name__ == "__main__":
main()
Need help with getting the correct date modified format in dd-mm-yyyy. I tried all the available online resources. Error Image: Background Information: The script moves the old file into an archived directory and renames it with the file name + modified date. The modified date is what I am trying to get and its correct format.
... View more
12-30-2019
08:59 AM
|
0
|
6
|
2179
|
POST
|
Hello Joe, Yes I used the exact same link as you and still didn't get anything. I stored it into a variable and it didn't work.
... View more
12-27-2019
09:57 AM
|
0
|
1
|
1866
|
POST
|
#!/usr/bin/env python
import requests
import os.path
import sys
import shutil
import pandas as pd
from datetime import datetime
def getFile():
url = "https://www.gosolarcalifornia.ca.gov/equipment/documents/Grid_Support_Inverter_List_Full_Data.xlsm"
content = requests.get(url)
date = datetime.date(datetime.now())
directory = "C:\Users\slee2\Folder"
archivedDirectory = "C:\Users\slee2\Folder\ArchivedFolder"
#date = datetime.datetime.fromtimestamp(os.path.getmtime(archivedDirectory)))
fileName = "Grid_Support_Inverter_List_Full_Data"
extension = ".csv"
path = fileName + extension
print ("Files will be saved in the following directory: "+directory+"\n")
print ("Old Files with the same name will be archived in the following directory: "+archivedDirectory+"\n")
print ("NOTICE: File will be converted to .csv file...")
if os.path.isdir(directory):
#Check if the Archived Folder Exists, if it does not create it
if(os.path.isdir(archivedDirectory)):
pass
else:
os.mkdir(archivedDirectory)
print ("SUCCESS: Directory was found.")
counter = 0
if (os.path.exists(directory+"\\"+fileName+extension)):
counter += 1
while (os.path.exists(archivedDirectory+"\\"+fileName+"_%s_%s"%(str(date), str(counter))+extension)):
counter += 1
if counter > 0:
print ("File: "+fileName+" already exists in directory,moving old file to Archived Folder. ")
shutil.move(directory+"\\"+fileName+extension, archivedDirectory+"\\"+fileName+"_%s"%(str(date))+extension)
with open(os.path.join(directory, path), 'wb') as f:
f.write(content.content)
deleteRows(directory, path)
print("Done.")
else:
print("ERROR: Directory is not found.")
sys.exit()
def deleteRows(directory, path):
newFile = pd.read_excel(os.path.join(directory,path))
newFile = newFile.drop(range(14), axis = 0)
newFile.to_csv(os.path.join(directory, path), encoding='utf8', header=False, index=False)
def main():
getFile()
if __name__ == "__main__":
main()
Sorry I didn't know how to paste code into these and now I do, thanks.
... View more
12-27-2019
09:36 AM
|
0
|
3
|
1866
|
POST
|
Hello, When I run my python script, it downloads a file from a website to a directory and it moves an old file from that directory to an archived folder and renames it with filename+date moved/date when script runs. I am trying to fix it to make it show filename+modified/created date instead of filename+date moved/date when script runs. This file is in the archived folder by the way. Attached is the python code.
... View more
12-27-2019
08:39 AM
|
0
|
14
|
3297
|
POST
|
Is it comparing the modified date or just date in general?
... View more
12-26-2019
09:42 AM
|
0
|
4
|
917
|
POST
|
Hello Blake, Thanks, I believe it works because I added an else statement, since the file on the website hasn't been updated yet. To clarify, we are comparing the date of those two files despite one of them being a csv file which is the file on the local drive and not the website, right?
... View more
12-26-2019
09:12 AM
|
0
|
6
|
917
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|