Sriram
Sriram

Reputation: 449

datepicker + webdriver + unable to identify the element

In my application i have a text box which when i click brings the date picker to select a date. One way is to select the date from datepicker or u can manually send in the date. But whenever i send in the date it says it is unable to locate the element.

THis is code for sending the date to the text box

driver.findElement(By.xpath(".//*[@id='txtdateFrom']")).sendKeys("03/05/2013");

It throws the following error

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='txtdateFrom']"}
Command duration or timeout: 15 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.31.0', revision: '1bd294d', time: '2013-02-27 20:52:59'
System info: os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_17'
Session ID: 3eea4ac2-2b38-4688-9733-8734077f7d3e
Driver info: org.openqa.selenium.firefox.FirefoxDriver

I dont have the privilege to add the screenshot. Please help!!

Please find the HTML below

<input id="txtdateFrom" class="textbox hasDatepicker" type="text" style="color:Gray;" name="txtdateFrom">

Please find the iframe HTMl

<iframe width="100%" scrolling="auto" height="493" frameborder="1" style="vertical-align: top;" allowtransparency="true" id="ContentMain" src="../Report/AuditorAssignmentReportSearch.aspx?Width=100&amp;Height=528"></iframe>

DOM of the text field which when clicked will populate datepicker

  attributes
    [type="text", style="color:Gray;", 2 more...]

0
    type="text"

1
    style="color:Gray;"

2
    class="textbox hasDatepicker"

3
    id="txtdateFrom"

4
    name="txtdateFrom"

and this is the attribute of the datepicker once the text field is clicked

attributes
    [class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all"]

0
    class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all"

Updated DOM

attributes
    [onclick="DP_jQuery_1368459704950.datepicker._selectDay('#txtdateFrom',4,2013, this);return false;", class=

"  ui-datepicker-today"

]

0
    onclick="DP_jQuery_1368459704950.datepicker._selectDay('#txtdateFrom',4,2013, this);return false;"

Upvotes: 0

Views: 4757

Answers (3)

Sriram
Sriram

Reputation: 449

First up thanks for the support and all the suggestions. The issue was with browser compatibility. Our Application works only in IE 8. The whole time i was using IE 9. When i checked my code in IE 8 it works perfectly and am able to enter the date. Thanks All!!

Upvotes: 0

ievche
ievche

Reputation: 1805

In my opinion you should use js executor:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.document.getElementById('txtdateFrom').setAttribute('value', '03/05/2013');");

Attribute may be different, you should search for it in DOM.

Or may be selenium doesn't wait for element and can't find it. Can you find this element without sending keys to it?

Upvotes: 1

goya
goya

Reputation: 165

Easiest approach would be to try driver.findElement(By.xpath(".//*[@id='txtdateFrom']")).click().sendKeys("03/05/2013"); Although sendKeys does that implicitly, sometimes a click into the Inputfield before sendKeys() is very useful, that's my experience. Further this xpath is ok but seems to be a copy&paste from firepath. I'd suggest you use //input[@id='txtdateFrom']. So it's more readable.

Upvotes: 0

Related Questions