开发者

Getting element in WebDriver, not by xpath

开发者 https://www.devze.com 2023-03-23 03:23 出处:网络
I am using webdriver for test automation on site which code is auto-generated probably in GWT. All id开发者_JS百科\'s are in form like \"x-auto-4009\" which is not to reliable way of getting to eleme

I am using webdriver for test automation on site which code is auto-generated probably in GWT. All id开发者_JS百科'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.


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().


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号