Reputation: 563
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 part of the form that says Date: FROM
. When I click it, a calendar pops up for you to pick dates. However, the field also allows you type in a date. Given the complexity of calendars, I have chosen to use send_keys()
but it is not working.
I have identified the empty field date field by its ID but for some reason it does not fill the form. when I try:
driver.find_element_by_id('ctl00_cphMain_SrchDates1_txtFiledFrom').send_keys("01012011")
Any ideas on how I can maneuver it differently? I'm using Python 2.7 with Selenium and ChromeDriver
Upvotes: 8
Views: 42435
Reputation: 11
date = 'yyyy-mm-dd'
driver.execute_script(f"document.getElementById('id').value = '{date}'")
Upvotes: -1
Reputation: 1903
An alternative solution with some explanation:
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
Here is a screenshot of devtools open to your page:
Two things stick out:
input
has value="From"
. So if you just call send_keys
right away the input will have value From01012011
.input
to have type=text
(it should really be type=date
!)So we have just ctrl-a, backspace
to clear the value.
Upvotes: 4
Reputation: 16993
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")
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.
Upvotes: 9
Reputation: 1383
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 :-)
Upvotes: -2