通过 Selenium Webdriver 处理 ID 一直在变化的元素 [英] Handle elements that have changing ids all the time through Selenium Webdriver

查看:30
本文介绍了通过 Selenium Webdriver 处理 ID 一直在变化的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行脚本来自动化测试用例并遇到这个独特的问题.我已检测并使用元素的 ID 进行点击等目的.然而,突然间这些 id 发生了变化,脚本不再起作用.另一个奇怪的事情是这些 ID 在 Chrome 中检查时与脚本中的相同,但在 Firefox 驱动程序浏览器中不同.

I am running the script to automate test cases and having this unique problem. I have detected and used IDs of the elements for click etc. purpose. However, all of a sudden these ids have changed and the script works no more. Another weird thing is those IDs are same as in script when inspected in Chrome but different in Firefox driver browser.

测试驱动程序的 Firebug: -

Firebug for test driver: -

<p class="description" onclick="selectElementTextListForIE(this,'tile29', 'tile19');selectElementTextList(this,'tile29', '')" id="tile29_span_0_0">
                                                            Platinum
                                                        </p>

相同元素的 Chrome 检查器:-

Chrome inspector for same element: -

<p class="description" onclick="selectElementTextListForIE(this,'tile20', 'tile19');selectElementTextList(this,'tile20', '')" id="tile20_span_0_0">
                                                            Platinum
                                                        </p>

此外,检测此类元素的最佳策略是什么,其 ID 是在运行时生成的.我什至尝试使用 XPATH 但它也包含 id 的引用例如.@id="tile276_input

Also, what could be the best strategy for detecting such elements whose IDs are generated on run. I even tried using XPATH but that too contains id's reference eg. @id="tile276_input

任何帮助将不胜感激.

谢谢.

阿布舍克

推荐答案

您可以为此使用 CSS.对于您的元素,看起来像它的:

You can utilize CSS for this. For your element, looks like its:

<* id="tile276_input" />

您需要做的是找出它发生了什么变化.我假设它是介于两者之间的数字.如果是,那么您的选择器将类似于:

What you need to do is find out what is changing about it. I assume it's the number inbetween. If it is, then your selector would look something like:

By.cssSelector("*[id^='tile'][id$='input']")

这将查找 ID 为以 tile 开头"和以 input 结尾"的任何内容.在我们的例子中,"tile276_input"匹配.

This will look for anything that has an ID that "starts with tile" and "ends with input. In our case, "tile276_input" matches that.

如果需要,请参阅这篇文章更多信息

See this article if you want more information

这篇关于通过 Selenium Webdriver 处理 ID 一直在变化的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆