Skip to content Skip to sidebar Skip to footer

Using Selenium On Calendar Date Picker

I am trying to pick a date (01/01/2011) from a calendar on this page. https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx The calendar is on the par

Solution 1:

To get this to work, add one extra step of clicking the element before sending the keys:

datefield = driver.find_element_by_id('ctl00_cphMain_SrchDates1_txtFiledFrom')
datefield.click()
datefield.send_keys("01012011")

Update:

It looks like you might have to use ActionChains after all in your case, which will allow you to chain a series of actions together, and then perform them one after the other:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()

driver.get("https://cotthosting.com/NYRocklandExternal/User/Login.aspx")
driver.find_element_by_id('ctl00_cphMain_blkLogin_btnGuestLogin').click()

driver.find_element_by_id('ctl00_cphMain_SrchNames1_txtFirmSurName').send_keys("Adam")

datefield = driver.find_element_by_id('ctl00_cphMain_SrchDates1_txtFiledFrom')

ActionChains(driver).move_to_element(datefield).click().send_keys('01012011').perform()
search_btn = driver.find_element_by_id('ctl00_cphMain_btnSearchAll')
ActionChains(driver).move_to_element(search_btn).click().click().perform()

I am not sure why two click() calls were necessary in this case, but it seems that they were. I tried a few other things including double_click(), but this was the only thing that worked for me to get the datefield unfocused and then click the search button.


Solution 2:

An alternative solution with some explanation:

Solution

Similar to what is suggested here, do:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver.get("https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx")

# [Do whatever else is necessary...]

date_input = driver.find_element_by_id('ctl00_cphMain_SrchDates1_txtFiledFrom')
date_input.click()                      # Focus input field
date_input.send_keys(Keys.CONTROL, "a") # Select all pre-existing text/input value
date_input.send_keys(Keys.BACKSPACE)    # Remove that text
date_input.send_keys("01012011")        # Add desired text/set input value

What is going on

Here is a screenshot of devtools open to your page:

devtools screenshot

Two things stick out:

  1. The input has value="From". So if you just call send_keys right away the input will have value From01012011.
  2. Parenthetically, it appears that they set input to have type=text (it should really be type=date!)

So we have just ctrl-a, backspace to clear the value.


Solution 3:

date = 'yyyy-mm-dd'
driver.execute_script(f"document.getElementById('id').value = '{date}'")

Solution 4:

I use Actions Class in Java . The Java code which ran successfully is as belows:

package stackoverflow;

public class question1 {

@Test
public void a () throws InterruptedException{

    System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe");
    WebDriver a = new ChromeDriver();
    a.manage().window().maximize();
    a.get("https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx");
    Thread.sleep(2000L);
    a.findElement(By.id("ctl00_cphMain_blkLogin_btnGuestLogin")).click();
    Thread.sleep(2000L);
    a.findElement(By.id("ctl00_cphMain_SrchDates1_txtFiledFrom")).click();
    Actions b = new Actions(a);
    for (int i = 0;i<6 ;i++){
        for(int j = 0; j<6; j++){   
                WebElement c = a.findElement(By.xpath("//*[@id='ctl00_cphMain_SrchDates1_ceFiledFrom_day_"+ i + "_" + j + "']"));
                Thread.sleep(2000L);
                b.moveToElement(c).build().perform();
            }
        }
    }
}

I tried to convert it for you in python, but I'm not sure about the syntax. Below is the code:

    a.get("https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx");
    a.implicitly_wait(3);
    a.find_element_by_id("ctl00_cphMain_blkLogin_btnGuestLogin").click();
    a.implicitly_wait(3);
    a.find_element_by_id("ctl00_cphMain_SrchDates1_txtFiledFrom").click();
    actions = ActionChains(a);
    for (int i = 0;i<6 ;i++){
        for(int j = 0; j<6; j++){   
                WebElement c = a.find_element_by_xpath("//*[@id='ctl00_cphMain_SrchDates1_ceFiledFrom_day_"+ i + "_" + j + "']");
                a.implicitly_wait(3);
                actions.move_to_element(c).perform();

            }
        }

Try it at your end and let me know for further issues. Happy learning :-)


Post a Comment for "Using Selenium On Calendar Date Picker"