硒RC:如何检查元素是否有一个给定的属性? [英] Selenium RC: How to check if an element has a given attribute?

查看:150
本文介绍了硒RC:如何检查元素是否有一个给定的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的onclick 属性的一些按钮和一些不。我要检查如果指定的元素具有的onclick 属性。我怎样才能做到这一点?

的getAttribute()返回属性值时,它有一个。当它没有,它抛出一个RuntimeException,并停止测试(甚至当我将它包装在一个try / catch块)。

  $ onclickValue = $这个 - >的getAttribute($ @定位的onclick); //当属性存在工作


解决方案

通过使用 getEval(),你可以执行JavaScript hasAttribute() 功能。使用 findElement(),将让你与任何类型的定位模式的工作。

  $ hasAttribute = $这个 - > getEval('this.browserbot.findElement('$定位。').hasAttribute(onclick事件)');
如果($ hasAttribute ==='真'){
    //属性存在
}

注意 getEval()返回一个字符串,而不是一个布尔值。

I have some buttons with an onclick attribute and some that don't. I want to check if the specified element has the onclick attribute. How can I do this?

getAttribute() returns the attribute value when it has one. When it doesn't, it throws a RuntimeException and stops the test (even when I wrap it in a try/catch block).

$onclickValue = $this->getAttribute("$locator@onclick"); //works when the attribute exists

解决方案

By using getEval(), you can execute the javascript hasAttribute() function. Using findElement(), will allow you to work with any type of locator pattern.

$hasAttribute = $this->getEval('this.browserbot.findElement("' . $locator . '").hasAttribute("onclick")');
if ($hasAttribute === 'true') {
    //attribute exists
}

Note that getEval() returns a string, not a boolean.

这篇关于硒RC:如何检查元素是否有一个给定的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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