sandeep kumar sandeep kumar - 4 months ago 13
Java Question

Unable to locate an element of date picker

I am unable to locate an element of date picker. I tried CssSelector and xpath and everything. Below is the code i tried.

Code :

WebElement dateWidget = driver.findElement(By.xpath(".//*[@id='dp1469511990123']"));
List<WebElement> columns=dateWidget.findElements(By.tagName("td"));

for (WebElement cell: columns)
{
if (cell.getText().equals("21"))
{
cell.findElement(By.linkText("21")).click();
break;
}
}


Here is HTML code.

HTML :

<input id="dp1469511990123" class="ng-pristine ng-untouched ng-valid >hasDatepicker ng-not-empty" type="text" ng-disabled="input.p1Edit==0" ng->model="vendor.companyStartedDate" ui-date="comp_st_date" disabled="disabled"/>


Here is the Error:


org.openqa.selenium.NoSuchElementException: Unable to locate element: >{"method":"xpath","selector":".//*[@id='dp1469511990123']"}
Command duration or timeout: 10.07 seconds
For documentation on this error, please visit: >http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: >'35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'user-PC', ip: '192.168.1.52', os.name: 'Windows 7', >os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.firefox.FirefoxDriver

Answer

The id value looks dynamically generated, I would use an ng-model instead:

driver.findElement(By.cssSelector("[ng-model='vendor.companyStartedDate']"));

Count improved readability as a bonus.

And, see if you need to wait until the presence of the element.