1
votes

all I'm delevoping a web app and I need to find a specific element but I can't find it out with my xpath. This is the html code where I'm trying to find the element

<table>
    <tr>
        <th class="details">
            <label>Name</label>
        </th>
        <td class="data">
            <div id="...">
                <div id="..."></div>
                <input id="..." type="text">
            </div>
        </td>
    </tr>
 ..........
</table>

I successfully found it but when I noticed that the ids are autogenerated I got stuck. I tried to solve this problem finding the label which contains the text 'Name' and then finding the siblings but nothing. This is my xpath:

//*[text()[contains(.,'Name')]]/../following-sibling::td
2
Try out this //input[@type='text'] and you misspelled Name as Nome in your xpath. - Ravikumar
The classes are also changing? like details, data - lauda
I forgot to say that in this website there are multiple input tags, however also the class are changing. - Umberto Palazzini

2 Answers

2
votes

You should try using below xpath :-

String xpath = "(.//label[text() = 'Name']/following::input)[1]";

Or

String xpath = ".//label[text() = 'Name']/../following-sibling::td//input";

Or

String xpath = "(.//input[preceding::label[text() = 'Name'])[1]";

Or

String xpath = ".//input[ancestor::td[preceding-sibling::th/label[text() = 'Name']]]";

You can use anyone of these above xpath to find desire input element as below :-

browser.findElement(By.xpath(xpath));
0
votes

try below xpath

"//label[contains(.,'Name')]/../following-sibling::td/div/input[@type='text']"

Assuming that the DOM structure remains constant.