0

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?

spcial
  • 1,529
  • 18
  • 40
  • 1
    `element.getTagName()` ? – har07 Aug 11 '15 at 12:49
  • 2
    `Element.getTagName()` will return the tagName – Madhan Aug 11 '15 at 12:49
  • 2
    It's hard to be sure how your existing XPath relates to your sample HTML, since the former has td's with labels, divs, and class attributes, and the latter doesn't. But if you want your XPath to select the different types of input element, you can use `*[self::input or self::textarea or self::div or self::select]`. – LarsH Aug 11 '15 at 13:41
  • Actually `element.getTagName()` was absolutely what I was searching for. Thanks! I think this will do the trick.. – spcial Aug 11 '15 at 14:41
  • @LarsH thanks for the information. I think this can help me to improve my xpath – spcial Aug 11 '15 at 14:43

0 Answers0