Identifying the object type of found xpath WebElement

248 views Asked by At

I have a dialog with several different input fields and types where I want to check according to the written name in front of the input field if the field is

  1. presented
  2. readonly

The difficulty is that I have differnt types of input I want to check:

  1. input
  2. textarea
  3. div
  4. select

So the HTML is simply like:

<tr>
    <td>Name</td>
    <td><input ...></td>
</tr>
<tr>
    <td>Gender</td>
    <td><select ...></td>
</tr>
<tr>
    <td>information</td>
    <td><textarea ...></td>
</tr>
...and so on...

My method gets only the name in front of the field and if it should be presented and readonly:

public boolean checkIfPresentedAndReadonly(String field, boolean present, boolean readonly){
        boolean result = false;

        try{
            WebElement element = find(By.xpath("//tr[td[@class='c1'][label[text()[contains(.,'"+field+"')]]]]/td[@class='c2']/div/*"));
            result = (checkForReadOnly(element) == readonly);
        }catch(NoSuchElementException e){
            return !present;
        }

        return result;
    }

My xpath returns any type (could be div, select, input etc.).... is there a nice way to find out the type of the WebElement? I thought about looking up the type and then doing some if/else statements to check the state depending on the input type like for type <input> I would check if it is possible to do a clear() and sendKeys("..") and then read the input so I can be sure that the element is writable.

Or are they any other possible solutions?

0

There are 0 answers