0

I am using webdriver for test automation on site which code is auto-generated probably in GWT. All id's are in form like "x-auto-4009" which is not to reliable way of getting to elements. I have page with something like form. It's like

  Label name    |   <---- Input ---->
  Label name    |   <---- Input ---->
  Label name    |   <---- Input ---->

Each new line is coded as new table in html. Can you tell me what is the best way of getting to specific Input in more generic way? I wrote a method that takes a label name and then it finds all elements by tag TR. Next it get's theese allRows and scans them for labelname. If it's found then i find within that row elements with tag input and that is my goal. It works fine but it takes some time to do find all those elements and loop through them. I don't want to use xpath or locating elements through those fragile id's. Can you recommend me any other way of doing that?

Thanks in advance, regards.

1
  • No. I had to write more to add this comment... Commented Aug 29, 2011 at 6:33

2 Answers 2

2

If you can't use xpath, I think the only other way to do this would be to alter the code for the page.

It is possible to set these GWT ID values so that they have a clearer and more consistent value. See: https://developers.google.com/web-toolkit/doc/latest/DevGuideUiCss#widgets.

Less appealing is the option to give each input a class or name attribute by which you'd identify them, and then search By.class() or By.name().

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

1 Comment

Ohohoo long time no see :) I've managed to deal with it just by using class selectors and than filtering the results. Anyway, thx for help :)
2

Why not xpath? That's the most direct way to do it. Your existing approach is the slower alternative.

Use this xpath selection: <label> with text containing the "name", then its parent <td>, then its parent <tr>, then the first <input> in that row:

WebElement input = driver.findElement(By.xpath(
    "//label[contains(text(), '"+name+"')]/../../input"
));

I have not tested this. Might have to adjust to your table structure, too.

2 Comments

Thank you for this post. But i was simply told to try and not use xpath in my code :)
No problem. Yeah, XPath's not pretty but she sure can cook!

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.