简体   繁体   中英

not able to select date from date picker

I have recorded selenium code for selecting date from date picker. While running the test case, the date picker pops up and highlights the selected date correctly. But the date is not selected. The code looks like:-

driver.findElement(By.id("imgStartDate")).click();
driver.findElement(By.xpath("//td[5]")).click();
driver.findElement(By.xpath("//td[5]")).click();
driver.findElement(By.xpath("//td[5]")).click();
driver.findElement(By.xpath("//td[5]")).click();

// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]

This exception appears in the recorded code only.I am using selenium-server-standalone-2.45.0 jar.

Recording will not work for date picker... try this code.

try{
    WebElement dateWidget = driver.findElement(By.xpath(OR.getProperty(object)));

    List<WebElement> rows = dateWidget.findElements(By.tagName("tr"));  
    List<WebElement> columns = dateWidget.findElements(By.tagName("td"));  

    for (WebElement cell: columns){
        if (cell.getText().equals(data)){
            cell.findElement(By.linkText(data)).click();
            break; 
        }
    }
}catch(Exception e){
    return Constants.KEYWORD_FAIL+" -- Not able to select the date"+e.getMessage();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM