在 Watir 中访问没有属性的元素 [英] Accessing an element with no attributes in Watir

查看:18
本文介绍了在 Watir 中访问没有属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Watir,有没有办法访问没有属性的元素?

Using Watir, is there a way to access an element without attributes?

例如:

<span>Text</span>

我想避免使用 xpath,但如果这是唯一的方法,那就太酷了.

I'd like to avoid using xpath, but if that's the only way it's cool.

推荐答案

首先忽略具有标签的非 WATIR 问题,或者向开发人员(或您自己)请求独特的属性,您始终可以通过以下方式访问元素其父元素,或按索引.

Disregarding the non-WATIR issues of having tags in the first place, or requesting unique attributes from your developers (or yourself), you can always access an element via its parent elements, or by index.

例如:文本

@browser.div(:name => "content").span(:index => 1)
#this is the first span element inside this div

在到达子 span 元素之前,您可以处理任何您需要的独特元素,而无需使用 Xpath.当然,您只需要一个唯一的父元素即可到达该特定的子元素,然后再从该元素向下传递到子元素.

You can work through however many unique elements you need to before reaching the child span element, without using Xpath. Of course, you only need one unique parent element to reach that specific child element, and you work down from that to the child.

div(:how => what).table(:how => what).td(:how => what).span(:how => what).text

另一个例子,假设它是页面上的 nth 个跨度:@browser.span(:index => n)

Another example, assuming it is the nth span on the page: @browser.span(:index => n)

然而,按索引方法非常脆弱,并且在对页面进行任何更新时容易中断.

The by-index approach is very brittle and prone to breaking when any update is made to the page, however.

这篇关于在 Watir 中访问没有属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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