通过Selenium Webdriver始终处理具有不断变化的id的元素 [英] Handle elements that have changing ids all the time through Selenium Webdriver

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

问题描述

我正在运行脚本来自动化测试用例,并遇到了这个独特的问题. 我已经检测并使用了元素ID来实现点击等目的.但是,这些ID突然间发生了变化,该脚本不再起作用. 另一个奇怪的是,这些ID在Chrome中检查时与脚本中的ID相同,但在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

任何帮助将不胜感激.

谢谢.

Abhishek

推荐答案

您可以为此使用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']")

这将查找具有以tile开头"和以input结尾"的ID的任何内容.在我们的示例中,"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天全站免登陆