在Calender图标的网页“http://www.makemytrip.com”上选择日期13/08/2014

时间:2014-05-03 06:50:20

标签: calendar selenium-webdriver

网页:http://www.makemytrip.com

我尝试过使用X-path但是我得到了No Such Element Exception。以下是代码:

driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div[2]/div/a/span")).click();


    /*DatePicker is a table.So navigate to each cell   
       * If a particular cell matches value 13 then select it  
       */  
      WebElement dateWidget = driver.findElement(By.id(".//*[@id='ui-datepicker-div']/div[1]"));  
      List<WebElement> rows=dateWidget.findElements(By.tagName("tr"));  
      List<WebElement> columns=dateWidget.findElements(By.tagName("td"));

      System.out.println("Rows count :" + rows.size());
      System.out.println("Column count :" + columns.size());

      for (WebElement cell: columns)
      {  
          //Select 13th Date   
          if (cell.getText().equals("13"))
          {  
              cell.findElement(By.linkText("13")).click();  
              break;  
          }  
      }

2 个答案:

答案 0 :(得分:0)

请更改您的代码

driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div[2]/div/a/span")).click();


/*DatePicker is a table.So navigate to each cell   
   * If a particular cell matches value 13 then select it  
   */  
WebElement dateWidget = driver.findElement(By.id(".//*[@id='ui-datepicker-div']/div[1]"));  

到这个

driver.findElement(By.xpath("//a[@tabindex='35']")).click();
WebElement dateWidget = driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div[1]"));  

您已使用id代替xpath。请改变它。

答案 1 :(得分:0)

我尝试了更新的makemetrip网站:

WebElement dateWidget = driver.findElement(By.xpath("//div[@id='js-filterOptins']"));  

dateWidget= dateWidget.findElement(By.xpath("//div[@class='dateFilter hasDatepicker']/div/div"));


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

System.out.println("Column count :" + columns.size());

for (WebElement cell: columns)
{  
     //Select 16th Date   
     if (cell.getText().equals("16"))
     {  
         cell.findElement(By.linkText("16")).click(); 
         System.out.println(cell);
         break;  
     }  
}