在 WebDriver 中获取元素,而不是通过 xpath
我正在使用 webdriver 进行现场测试自动化,其代码可能是在 GWT 中自动生成的。 所有 id 的形式都类似于“x-auto-4009”,这不是获取元素的可靠方法。 我有类似表格之类的页面。 一样
Label name | <---- Input ---->
Label name | <---- Input ---->
Label name | <---- Input ---->
就像每一个新行都被编码为 html 中的新表 。 您能告诉我以更通用的方式获取特定输入的最佳方法是什么吗? 我编写了一个方法,该方法采用标签名称,然后通过标签 TR 查找所有元素。接下来,它获取这些所有行并扫描它们的标签名称。如果找到,那么我会在该行元素中找到带有标签输入的元素,这就是我的目标。它工作正常,但需要一些时间才能找到所有这些元素并循环遍历它们。 我不想使用 xpath 或通过那些脆弱的 id 定位元素。 你能推荐我任何其他方法吗?
提前致谢, 问候。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不能使用 xpath,我认为唯一的其他方法是更改页面的代码。
可以设置这些 GWT ID 值,使它们具有更清晰、更一致的值。请参阅:https://developers.google.com/web-toolkit/doc /latest/DevGuideUiCss#widgets。
不太吸引人的是为每个输入提供一个类或名称属性,您可以通过它来识别它们,然后搜索
By.class()
或By.name()
。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()
orBy.name()
.为什么不用xpath?这是最直接的方法。您现有的方法是较慢的选择。
使用此 xpath 选择:
以及包含“名称”的文本,然后是其父级
,然后是其父级
< /code>,然后是该行中的第一个
:
我还没有对此进行测试。可能还需要调整您的表结构。
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:I have not tested this. Might have to adjust to your table structure, too.