1

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
3
  • Try out this //input[@type='text'] and you misspelled Name as Nome in your xpath. Commented Sep 4, 2016 at 17:54
  • The classes are also changing? like details, data Commented Sep 4, 2016 at 17:56
  • I forgot to say that in this website there are multiple input tags, however also the class are changing. Commented Sep 4, 2016 at 18:17

2 Answers 2

2

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));
Sign up to request clarification or add additional context in comments.

Comments

0

try below xpath

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

Assuming that the DOM structure remains constant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.