0

I want to set the date value by selenium.

Which is a datapicker element with disabled attribute

This script doesn't work for me

@driver.execute_script("document.getElementById('departureDate').setAttribute('value', '2015-09-07')")

I even couldn't get value by this @driver.execute_script("document.getElementById('departureDate').getAttribute('value')")

However I can get inspect the value by

[50] pry(#<Tiger>)> el = @driver.find_element(:id, "departureDate")
#<Selenium::WebDriver::Element:0x3f5415a2252a05c4 id="{f12a4095-6326-d241-b22d-aca9c03918e3}">
[51] pry(#<Tiger>)> el.attribute("value")
"2015-09-18"

But how can I set the value with 2015-09-07

I'm working on this page https://m.tigerair.com/booking/search

newBike
  • 14,385
  • 29
  • 109
  • 192

1 Answers1

1

Use querySelector() by supplying css path to get tag information, and then add this js-code to selenium as a string.

var taginfo = document.querySelector('input#departureDate');
var date = taginfo.value;
console.log(date);

to change try this.

taginfo.value.replace('2015-09-08'); 
Community
  • 1
  • 1
Yash
  • 9,250
  • 2
  • 69
  • 74
  • How could I add this js library to ruby selenium, thanks – newBike Sep 07 '15 at 06:02
  • read js code as String and supply to executeScript() @driver.execute_script(jsAsString); [see this](http://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-using-java/31293581#31293581) – Yash Sep 07 '15 at 06:14
  • js_code = "return document.querySelector('input#departureDate')" tagInfo = browser.execute_script(js_code) date = tagInfo .attribute("value") [here](http://stackoverflow.com/questions/20869034/selenium-parse-elements-to-string) – Yash Sep 07 '15 at 06:23
  • Shouldn't i include the js lib in the website or do something with Ruby-binding selenium ? but the website is not owned by me. – newBike Sep 07 '15 at 06:31
  • no need to include any js lib already this querySelector() is available in browsers. – Yash Sep 07 '15 at 06:53