0

I need to perform validation against the table that is being displayed like this.

daysTableCheck

code sample how I'm trying to find the checkbox Element

   //navigate t the second tr where the input and img tags are stored
    List<WebElement> daysCheckBox = this.driver.findElements(By.xpath(".//*[@id='RULE_KEY']/div/div/div/div/div/center[1]/table[2]/tbody/tr[2]/td")); 

    System.out.println("Test checkbox");
    for(WebElement td : daysCheckBox){
        System.out.println(td.getTagName());

    }

The problems I have is that this table come with 2 tags first row represents the days and the second "checkboxes" as you can see they line up with days, however checkbox have no link to each day. I have tried to solve this with reverse logic to try to identify the input tag if it is disabled. But when the box is selected it turns from input tag into img tag, also when I identify input tags I cannot pinpoint what day deselected box it corresponds to.

Does anyone have any advice or suggestion how to approach this type of validation? Thank you.

I have put the source to make it clearer how the picture above is presented.

<tbody>
 <tr bgcolor="whitesmoke">
   <td><b>Sun</b></td>
   <td><b>Mon</b></td>
   <td><b>Tues</b></td>
   <td><b>Wed</b></td>
   <td><b>Thurs</b></td>
   <td><b>Fri</b></td>
   <td><b>Sat</b></td>
 </tr>
 <tr>
   <td><img src="webwb/dhtmlx_iconcheckall.gif" /></td>
   <td><input disabled="disabled" type="checkbox" /></td>
   <td><input disabled="disabled" type="checkbox" /></td>
   <td><input disabled="disabled" type="checkbox" /></td>
   <td><img src="webwb/dhtmlx_iconcheckall.gif" /></td>
   <td><img src="webwb/dhtmlx_iconcheckall.gif" /></td>
   <td><input disabled="disabled" type="checkbox" /></td>
 </tr>
</tbody>
0

3 Answers 3

2

You can create method which will convert Your table to Java Map for example:

public static Map<String,String> getTableAsMap(WebDriver driver)
{
    Map<String,String> checkboxMap = new TreeMap<>();

    List<WebElement> header = driver.findElements(By.xpath("((//tbody/tr)[1])/td/*"));
    List<WebElement> checkboxes = driver.findElements(By.xpath("((//tbody/tr)[2])/td/*"));

    for(int i = 0;i<header.size();i++){

    //Only for testing purpose 
    System.out.printf("KEY: %s, VALUE: %s\n",header.get(i).getText(),checkboxes.get(i).getAttribute("disabled"));

    checkboxMap.put(
                header.get(i).getText(),
                checkboxes.get(i).getAttribute("disabled")
        );
    }
    return checkboxMap;
}

Then In your main class with driver use:

Webdriver driver = new FirefoxDriver();
Map<String,String> map = getTableAsMap(driver);

System.out.println(map.get("Fri"));
System.out.println(map.get("Sat"));

For disabled (non selected) checkbox You will receive true for selected there will be null. Both as String.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I'm actually trying to do. The only part I was missing is the getAttribute("disabled") Thank you for the time to write up the code this solves this pesky implementation of the checkbox.
2

as you haven't posted the code, my answer also doesn't have a code :)

  1. Create the WebElement for second row, i.e. xpath: //tbody/tr[2]
  2. Now traverse all the tds inside this row and check their child element.
  3. If the child element is input you know that checkbox is not selected else it is.
  4. About for which day, you know the first is Sunday and Second is Monday and so on..

2 Comments

Thank you for the quick answer I will try to do that and post feeedback of result as well as the sample code
I included the code sample how I'm identifying the elements but I not quite sure how to check for the checkbox. when i do td.getTagName() I get td tags (7 of them). do you now the method to get the input tag instead? Thank you
0

The Page Object Model approach may be effective to deal with this scenario. So, you can refresh your page object after some action/set of actions.

Since you mentioned that once a checkbox is checked the tag gets changed for the element, so I assume this can happen multiple times in the page based on user activity.

I can suggest that you implement a callback that reloads the and set the page objects on user action say click. With the page objects, you always have track of all the WebElements, accessibility and any change of state.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.