Selenium python script
from selenium import webdriver
import datetime
import time
from datetime import timedelta
def data(start_DD,start_Mmm,start_YYYY,end_DD,end_Mmm,end_YYYY):
# help(data(bar))
''' enter date month and year
date=DD,month=May,year=2021 '''
#time.sleep(5)
driver = webdriver.Chrome() #get driver start here
driver.get('https://www.fpi.nsdl.co.in/web/Reports/Archive.aspx') # address here
start_date = datetime.datetime(start_YYYY,start_Mmm,start_DD)+timedelta(-1) #decrese by one day before entering while loop as while loop increase by ne day
end_date = datetime.datetime(end_YYYY,end_Mmm,end_DD)
while(start_date!=end_date):
start_date =start_date+timedelta(1)
# this is while loop that work from start_date to end_date
first_3_char_month=str(start_date.strftime("%B"))[0:3]
#print(first_3_char_month)
#print(start_date.strftime("%Y"))
#print(start_date.strftime("%d"))
driver.switch_to_window(driver.window_handles[0]) #switch to opened addresas of first tab
month=str(first_3_char_month) #get month valu here
day=str(int(start_date.strftime("%d"))) #get day value her
year=start_date.strftime("%Y") #get year value here
#print(year)
#print(day)
# Step 1: Create a session and load the page
#driver = webdriver.Chrome()
#driver.get('https://www.fpi.nsdl.co.in/web/Reports/Archive.aspx')
##driver.maximize_window()
#driver.switch_to_window(driver.window_handles[0])
#driver.switch_to_window("Archive" )
#driver.get('https://www.fpi.nsdl.co.in/web/Reports/Archive.aspx'))
# Wait for the page to fully load
#date select
driver.implicitly_wait(5)
driver.find_element_by_id("imgtxtDate").click()
time.sleep(2)
driver.find_element_by_xpath("/html/body/table/tbody/tr/td/div/div[1]/table/tbody/tr/td/div/div").click()
time.sleep(2)
e=driver.find_element_by_xpath("/html/body/table/tbody/tr/td/div/div[3]/table/tbody/tr/td/table[1]/tbody/tr[1]/td/input")
time.sleep(2)
e.clear()
#time.sleep(2)
e.send_keys(year)
# print(year)
time.sleep(1)
month_x="//div[contains(text(),"+"'"+month+"')]"
month_y=eval('"'+month_x+'"')
time.sleep(1)
driver.find_element_by_xpath(month_y).click()
if(int(day)<10): #if else for 1 to 9 day range as it give multiple value so used not contains x_pat method
day_x="//tr/td/div[contains(text(),"+"'"+day+"') and not(contains(text(),'10')) and not(contains(text(),'11')) and not(contains(text(),'12')) and not(contains(text(),'13')) and not(contains(text(),'14')) and not(contains(text(),'15')) and not(contains(text(),'16')) and not(contains(text(),'17')) and not(contains(text(),'18')) and not(contains(text(),'19')) and not(contains(text(),'20')) and not(contains(text(),'21')) and not(contains(text(),'22')) and not(contains(text(),'23')) and not(contains(text(),'24')) and not(contains(text(),'25')) and not(contains(text(),'26')) and not(contains(text(),'27')) and not(contains(text(),'28')) and not(contains(text(),'29')) and not(contains(text(),'30')) and not(contains(text(),'31'))]"
else:
day_x="//tr/td/div[contains(text(),"+"'"+day+"')]"
day_y=eval('"'+day_x+'"')
time.sleep(1)
driver.find_element_by_xpath(day_y).click()
yy=driver.find_element_by_xpath("//*[@id='hdnDate']").get_attribute('value')
date_response=yy
yy=str(str(yy)[0:6])[3:6] # it takes 3 leter of month of date_response
#date select if second index xpath is needed
if(yy!=month): #if month of response_date is not equal to inputed month from program then select second index value of xpath for day selection
driver.implicitly_wait(5)
driver.find_element_by_id("imgtxtDate").click()
time.sleep(2)
driver.find_element_by_xpath("/html/body/table/tbody/tr/td/div/div[1]/table/tbody/tr/td/div/div").click()
time.sleep(2)
e=driver.find_element_by_xpath("/html/body/table/tbody/tr/td/div/div[3]/table/tbody/tr/td/table[1]/tbody/tr[1]/td/input")
time.sleep(2)
e.clear()
#time.sleep(2)
e.send_keys(year)
#print(year)
time.sleep(1)
month_x="//div[contains(text(),"+"'"+month+"')]"
month_y=eval('"'+month_x+'"')
time.sleep(1)
driver.find_element_by_xpath(month_y).click()
if(int(day)<10):
day_x="(//tr/td/div[contains(text(),"+"'"+day+"') and not(contains(text(),'10')) and not(contains(text(),'11')) and not(contains(text(),'12')) and not(contains(text(),'13')) and not(contains(text(),'14')) and not(contains(text(),'15')) and not(contains(text(),'16')) and not(contains(text(),'17')) and not(contains(text(),'18')) and not(contains(text(),'19')) and not(contains(text(),'20')) and not(contains(text(),'21')) and not(contains(text(),'22')) and not(contains(text(),'23')) and not(contains(text(),'24')) and not(contains(text(),'25')) and not(contains(text(),'26')) and not(contains(text(),'27')) and not(contains(text(),'28')) and not(contains(text(),'29')) and not(contains(text(),'30')) and not(contains(text(),'31'))])[2]"
else:
day_x="(//tr/td/div[contains(text(),"+"'"+day+"')])[2]"
day_y=eval('"'+day_x+'"')
time.sleep(1)
driver.find_element_by_xpath(day_y).click()
date_response_2nd_index=driver.find_element_by_xpath("//*[@id='hdnDate']").get_attribute('value')
print("date_response_2nd_index : "+date_response_2nd_index)
else :
print("")
#print(yy+" got error on second index x_path")
time.sleep(1)
#date select
#getdate derspons
print("inputed_date : "+str(start_date))
print("date_response : "+date_response)
#click view report and export to excel
driver.find_element_by_xpath("//div[contains(text(),'View Report')]").click()
driver.find_element_by_xpath("//div[contains(text(),'Export to Excel')]").click()
time.sleep(5)
#driver.close()
data( 28 ,7 , 2020, 31, 7, 2020)
No comments:
Post a Comment