0

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;"
4

3 回答 3

1

在我看来,你应该使用 js 执行器:

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

属性可能不同,你应该在 DOM 中搜索。

或者可能是硒不等待元素并且找不到它。你能在不发送密钥的情况下找到这个元素吗?

于 2013-05-13T13:53:16.660 回答
0

首先感谢您的支持和所有建议。问题在于浏览器兼容性。我们的应用程序只能在 IE 8 中运行。我一直在使用 IE 9。当我在 IE 8 中检查我的代码时,它运行良好并且能够输入日期。谢谢大家!!

于 2013-05-17T09:55:07.167 回答
0

最简单的方法是尝试 d​​river.findElement(By.xpath(".//*[@id='txtdateFrom']")).click().sendKeys("03/05/2013"); 虽然 sendKeys 会隐式执行此操作,但有时在 sendKeys() 之前单击输入字段非常有用,这是我的经验。此外,此 xpath 还可以,但似乎是从 firepath 复制和粘贴的。我建议你使用 //input[@id='txtdateFrom']。所以它更具可读性。

于 2013-05-12T16:55:38.920 回答