How to Input Date Using Sendkeys in Selenium Webdriver

Input date using Selenium

Input field #popupDatepicker has attribute readonly. That means that you cannot send keys to it.

Try below to select required date

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium import webdriver


driver = webdriver.Firefox()
driver.get('https://www.nccpl.com.pk/en/market-information/fipi-lipi/lipi-sector-wise-daily')
picker = wait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'popupDatepicker')))
driver.execute_script('arguments[0].scrollIntoView();', picker)
picker.click()
wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[title="Select Tuesday, Jan 1, 2019"]'))).click()

If you need to select month or year you need to handle select drop-down:

from selenium.webdriver.support.ui import Select 

select_month = Select(wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[title="Change the month"]'))))
select_month.select_by_visible_text('April')
select_year = Select(wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[title="Change the year"]'))))
select_year.select_by_visible_text('2018')

How to send dates to read-only calendar input boxes using Selenium webdriver?

You can use below lines instead send_keys to write date in date input field. Basically we directly changing value of element.

element= browser.find_element_by_xpath('//*[@id="bpcg9kk"]/div/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/input')

browser.execute_script("arguments[0].setAttribute('value', ‘“Jan 01,2019"')", element);

OR



browser.execute_script(“arguments[0].value=arguments[1]", element, “Jan 01,2019”)

Another solution:

Make input field as editable by removing readonly attribute and then send keys as below

element= browser.find_element_by_xpath('//*[@id="bpcg9kk"]/div/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/input')


browser.execute_script("arguments[0].removeAttribute('readonly','readonly')",element)

element.send_keys("Jan 01,2019")

Clear field:

element2.send_keys(Keys.CONTROL + "a");
element2.send_keys(Keys.DELETE);

How to Automate HTML5 Date picker using Selenium?

There will be many approaches but suggesting sendKeys approach.

If we see the DOM of the Date Picker control, there is only input box for date.
To handle this type of control first fill date with string, i.e. if date format is MM/dd/yyyy, then pass 09282018 to the input box. Note, this will be depends on your date format.

WebElement date = driver.findElement(By.xpath("//form//input[@name='date']"));
//Fill date as format, ie, MM/dd/yyyy
date.sendKeys("09282018");
date.submit();

How to select specific date with Date Picker In Selenium WebDriver

Thank you guys for responded to my query... I have tried with Xpath and its works for me

Here the details:

With Below code i have been selecting Start & End date in "Date Picker"

driver.findElement(By.id("LeaveStartDate")).click();
driver.findElement(By.className("next")).click();

driver.findElement(By.xpath("html/body/div[8]/div[1]/table/tbody/tr[3]/td[2]")).click();
driver.findElement(By.id("LeaveSEndDate")).click();
driver.findElement(By.xpath("html/body/div[9]/div[1]/table/tbody/tr[3]/td[5]")).click();

Conclusion: Xpath is the solution for those who are unable to execute their selenium Java code of "Jquery Date Picker".

How to choose a specific date (when: type = "date") in Selenium WebDriver?

You should use a valid Date format like yyyy-mm-dd

driver.findElement(By.id("dob")).sendKeys("2013-04-11");

This will work in Firefox as well



Related Topics



Leave a reply



Submit