Web表迭代 - Java Selenium

时间:2018-03-16 17:25:08

标签: java xpath selenium-webdriver html-table webdriver

我正在尝试遍历日历网表,如果日历日期大于或等于当前日期,请单击与其对应的单选按钮。其余的代码可以工作但是。

更新代码

dowhileloop: do {
   // holds dates from 5th column in list
   List < WebElement > payDates = driver.findElements(By.xpath("//table[@id='changeStartWeekGrid_rows_table']//tr[position()>1]/td[position()=5]"));

   // prints out dates from 5th column in list
   List < String > texts = payDates.stream().map(WebElement::getText).collect(Collectors.toList());
   System.out.println("date ->" + texts);

   //Begin for-loop
    for (WebElement pd: payDates) {
    System.out.println("sample1-> " + pd.getText());

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    Date payDate = dateFormat.parse(pd.getText());
    System.out.println("sample2-> " + dateFormat.format(payDate));

    if (payDate.after(new Date())) {
     System.out.println("inside for loop");

     String radiobutton = "//TBODY[@id='changeStartWeekGrid_rows_tbody']/TR[7]/TD[1]/DIV[1]/DIV[1]/DIV[1]";
     WebElement calrow = driver.findElement(By.xpath(pd + radiobutton));

     calrow.click();
     Thread.sleep(10000);

     PS_OBJ_CycleData.donebtn(driver).click();
     break dowhileloop;
    }

   } //** END third inner for-loop****

----请求单选按钮HTML代码---- enter image description here

 < div id = "changeStartWeekGrid.store.rows[5].cells[0]_widget"
tabindex = "0"class = "revitRadioButton dijitRadio dijitRadioChecked"
onfocus = "var registry = require('dijit/registry'); registry.byId('changeStartWeekGrid').changeActiveCell('changeStartWeekGrid_row_5_cell_0', event);"
onblur = "var registry = require('dijit/registry'); registry.byId('changeStartWeekGrid').blurActiveCell('changeStartWeekGrid.store.rows[5]', 'changeStartWeekGrid.store.rows[5].cells[0]');" >
 < div class = "revitRadioButtonIcon" > < /div> < /div>

-----附加单选按钮HTML代码----  enter image description here

<span class="dndBar checked" data-tag="dndBar" id="changeStartWeekGrid.store.rows[5].cells[0]_img">
<i class="fa fa-ellipsis-v"></i><i class="fa fa-ellipsis-v"></i></span>

------------ console view ----

enter image description here

2 个答案:

答案 0 :(得分:1)

添加了伪代码......希望有所帮助

List<WebElement> payDates = driver.findElements(By.xpath("//tr[starts-with(@id,'changeStartWeekGrid_row_')]/td[contains(@id,'_cell_4')/span]"));

int reqIndex = 0;

for(WebElement pd;payDates) {

//Use class java.time.LocalDate to do the parsing andcomparison with current date.
if (condition satisfied) 
     break
else
     reqIndex++;
}

driver.findElement(By.xpath("//tr[@id='changeStartWeekGrid_row_'" + reqIndex +"]")/path to radio button).click()

答案 1 :(得分:0)

/* The below code doesn't loop through the table. The logic is , The Calendar Webtable starts from Sunday and the First Element that is clickable is the first day of the month. So based on which is the first clickable element and the date to select, the getRowColumnIndex function tells which row and column to select. This way you never traverse through the Webtable and the operation is faster*/
   public static void selectCalendarDate(WebDriver driver, String tableId, int dateToSelect) {
        Map<String,Integer> daysMap= new HashMap<String,Integer>();
        daysMap.put("SUNDAY", 0);
        daysMap.put("MONDAY", 1);
        daysMap.put("TUESDAY", 2);
        daysMap.put("WEDNESDAY", 3);
        daysMap.put("THURSDAY", 4);
        daysMap.put("FRIDAY", 5);
        daysMap.put("SATURDAY", 6);
        int firstElement = daysMap.get(LocalDate.now().minusDays(LocalDate.now().getDayOfMonth()-1).getDayOfWeek().toString());
        int rowColumn[] = getRowColumnIndex(dateToSelect, firstElement);
        rowColumn[0]= rowColumn[0]+2;
        WebElement elementtoClick= driver.findElement(By.xpath(tableId+"/tbody[1]/tr["+rowColumn[0]+"]/td["+rowColumn[1]+"]")); 
        elementtoClick.click();
        }
        public static int[] getRowColumnIndex(int date, int firstElement) {
        int row = 0, column = 0;

        if (firstElement > 0) {
            date = date + firstElement;
            row = (date % 7) > 0 ? (date / 7) + 1 : (date / 7);
            column = ((date % 7) == 0) ? 7 : (date % 7);
        } else {
            row = (date / 7) ;
            column = ((date % 7) == 0) ? 7 : (date % 7);
        }
        int rowcolumn[] = { row, column };
        return (rowcolumn);
    }