【问题标题】:Locating element in DOM for automated test case在 DOM 中为自动化测试用例定位元素
【发布时间】:2018-05-12 08:12:17
【问题描述】:

为了在 DOM 中定位元素,我使用以下路径:

 div[id^=customswindow-][id$=-body]>div>div:nth-child(3)>div>div>div>div>div>div>div>div>div>div>div>div>input

有没有办法缩小 div 的序列? div>div>div>div>div>div>div>div>div>div>div>div>

【问题讨论】:

  • 取决于页面结构,尤其是该路径上的任何类或 ID。如果你是手动编写的,我相信你可以清理很多。我写过类似的东西,但是以编程方式走上 dom 来创建它们,只是因为不负责结构
  • 共享相同的 HTML 代码示例
  • 你能分享你的html示例吗?
  • Watir 的一个目标是让您无需使用 XPath 来定位您的元素。如果您要查找的元素是输入,则无需深入研究即可找到它。根据您在页面上的输入,这让您有多接近? browser.div(id: /^customswindow-/).input
  • @titusfortner 我认为逻辑上的css 或xpath 也是如此。

标签: javascript ruby selenium automation watir


【解决方案1】:

嗯,解决任务/问题时没有定义最佳实践。话虽如此,您使用的cssSelector是基于它的Absolute Path。基于Absolute Path 的cssSelector 和xpath 往往很脆弱。因此我们需要构造Logical Path。

尽管cssSelector 您已用作:

div[id^=customswindow-][id$=-body]>div>div:nth-child(3)>div>div>div>div>div>div>div>div>div>div>div>div>input

我建议开始只查看 <input> 标记及其属性。您可以使用下面提到的任何方法:

//using id and other attribute
input#id[attribute=value]

//using classname and other attribute
input.classname[attribute=value]

//using id, classname and other attribute
input#id.classname[attribute=value]

//using three different attributes
input[attribute1=value][attribute2=value][attribute3=value]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    相关资源
    最近更新 更多