0

I am trying to pick a date on a date picker field and I want to be able to do this by firing javascript on selenium webdriver. I tried the below, which works by being able to go to the date window, however I thought there should be a way to just fire a javascript in @driver.execute_script(). Any javascript experts to help here please?

require 'selenium-webdriver'


@browser = Selenium::WebDriver.for :chrome

@browser.navigate.to 'http://adam.goucher.ca/parkcalc/'

@browser.find_element(:xpath => "//img[@alt='Pick a date']").click

#collect all window handles
window_handles =  @browser.window_handles.length

# printing the window ids
@browser.window_handles.map do |window|
  p window
end

@browser.switch_to.window(@browser.window_handles.last)

@browser.find_element(:link => '4')

#sleep to watch that indeed the date is picked
sleep 5

@browser.quit
machzqcq
  • 1,029
  • 13
  • 15
  • Do you want to pick up both the dates ? – Arup Rakshit Oct 04 '14 at 18:17
  • 1
    Can you clarify what you want to do with JavaScript? You say you want to pick a date, but that could be interpreted in a lot of ways. For example, do you want to specify a date without opening the popup at all or once the popup is displayed clicking a link with JavaScript or once the popup is opened setting the date without clicking a link or etc.? – Justin Ko Oct 04 '14 at 20:41
  • @JustinKo: Without opening the pop up, I don't think we can pick a date from the calendar widget [Typing date as text is NOT what I want]. So I DO want to open the pop up and after that fire the javascript. – machzqcq Oct 05 '14 at 05:09

2 Answers2

0

Looking at the source of the pop-up calendar it looks like the following will simulate picking a date (copied and pasted from the site with double semicolons):

winMain.document.getElementById('EntryDate').value='10/6/2014';;window.close();

Just substitute your date.

Details on using JavaScript with Selenium: https://stackoverflow.com/a/11439939/4108388

It may also be simpler to fill the date fields with text rather than using JavaScript: Selenium Webdriver: Entering text into text field

Community
  • 1
  • 1
fatcomma
  • 11
  • 5
  • Thank you for the valuable comment. I tried by copying the javascript as you mentioned in the code, however that doesn't pick the date. In fact throws the error "unknown error: winMain is not defined (Selenium::WebDriver::Error::UnknownError)" – machzqcq Oct 05 '14 at 05:11
  • OK a small change in the above line of code and it worked as below. winMain is not required. @browser.execute_script("document.getElementById('EntryDate').value='10/6/2014';;window.close();"). Thank you all for your help. – machzqcq Oct 05 '14 at 05:14
0

If you would like to parameterize the input date, you can use the below

input_date = 10/6/2014

@browser.execute_script("document.getElementById('EntryDate').value='#{input_date}';;window.close();")