Home > Back-end >  Selenium - Problems while selecting date in webpage
Selenium - Problems while selecting date in webpage

Time:04-20

I am trying to select some data from webpage: https://indiawris.gov.in/wris/#/groundWater I am facing problem while picking the start and end dates in the webpage. I have made an attempt using a code similar to this earlier, but here it did not work.

#code snippet tried for date picking earlier which is not working now
    startdate_ele = driver.find_element_by_id('start_date_1')
    startdate_ele.clear()
    startdate = '2016/11/01' ## set the start date
    end_date_ele = driver.find_element_by_id('end_date_1')
    end_date_ele.clear()
    endDate ='2017/07/31'   ## set the end date for files to download
    startdate_ele.send_keys(startdate)
    time.sleep(5)
    end_date_ele.send_keys(endDate)

Following is the current code.

#functional code that does selection prior to the date picking

from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select

path2 = r"F:\\chromedriver_win32\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=path2)
driver.maximize_window()

wait = WebDriverWait(driver, 30)

driver.get("https://indiawris.gov.in/wris/#/groundWater")

# try:
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@class='ng-star-inserted']")))
print('Switched successfully to iframe')
ele = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='views']//label[contains(.,'Basin')]//input")))
driver.execute_script("arguments[0].click();", ele)
print('Clicked on basin button')
sourceDrop = driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[2]')
sourceItems = sourceDrop.find_elements_by_class_name('ng-star-inserted')
print("length",len(sourceItems))
actionChains = ActionChains(driver)
select = Select(driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[2]/select'))
select.select_by_visible_text('CGWB')

select = Select(driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[3]/select'))
select.select_by_visible_text('yyyyMMdd')

startdate_ele = driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[4]/datepicker/div/input')
startdate_ele.clear()   
startdate = '03-January-2021' ## set the start date
end_date_ele = driver.find_element_by_id('end_date_1')
end_date_ele.clear()
endDate ='2017/07/31'   ## set the end date for files to download
startdate_ele.send_keys(startdate)
time.sleep(5)
end_date_ele.send_keys(endDate)

can some one help me in picking the dates from this portal to do the selection.

CodePudding user response:

There are several issues currently wrong with your code.

1. Use By.XPATH, By,CLASS_NAME, By.ID all of your items are depreciated.
2. select = Select(driver.find_element(By.XPATH,'//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[3]/select'))
select.select_by_value('yyyyMMdd')

Using the wrong selection for the timestep

3. Target the input tag and send keys for start and stop they have something like

<input _ngcontent-qlb-c14="" bsdatepicker=""  readonly="" ng-reflect-klass="form-control" ng-reflect-ng- ng-reflect-bs-config="[object Object]" ng-reflect-is-disabled="true" ng-reflect-max-date="Sat Jan 09 2021 00:00:00 GMT-0" ng-reflect-name="to" placeholder="Select to date" style="">

CodePudding user response:

For selecting Months below code will work to navigate w.r.t year we need to add more logic.

eleq = wait.until(EC.element_to_be_clickable((By.XPATH,"(//span[@class='input-group-addon pointer'])[1]")))
driver.execute_script("arguments[0].click();", eleq)

driver.find_element_by_xpath("//span[contains(text(),'January')]").click()

#eleq = wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@class='input-group-addon pointer']")))
eleqy = wait.until(EC.element_to_be_clickable((By.XPATH,"(//span[@class='input-group-addon pointer'])[2]")))
driver.execute_script("arguments[0].click();", eleqy)

driver.find_element_by_xpath("//span[contains(text(),'January')]").click()
  • Related