如何完全匹配使用:类选择器在watir-webdriver [英] how to exactly match using :class selector in watir-webdriver

查看:160
本文介绍了如何完全匹配使用:类选择器在watir-webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Watir 0.5.x
的向后不兼容更改查找元素:class现在匹配部分类属性。



如何获得旧的行为?也就是说以匹配确切的类名。



这是打破我的代码,在一种情况下:

 #expand all multiple records 
while $ browser.div(:class =>expander)。exists?
$ browser.div(:class =>expander)。click
end


$ b b

因为一旦 .click ed, div class strong>expander hasChildren(以及可能的其他类,具体取决于表行的颜色和内容)。

解决方案

要匹配确切的类名,您可以使用带有开头和结尾锚的正则表达式:

  $ browser.div(:class => / ^ expander $ / ).exists? 

这将完全匹配expander,而不是expander hasChildren p>

对于你的其他问题,如果有一个更好的方法来展开所有行,我认为这确实取决于页面的HTML是什么样子。你有例子吗?当我有一个类似的问题与类似的控件,我能够使用divs周围的可点击的结构,使搜索更具体。


Regarding the backwards-incompatible change of Watir 0.5.x Finding elements by :class now matches partial class attributes.

How do I get the old behavior? I.e. to match exact class name.

It is breaking my code, in one case:

# expand all multiple records
 while  $browser.div(:class => "expander").exists?  
  $browser.div(:class => "expander").click
 end

because once .clicked, the div's class becomes "expander hasChildren" (and possibly other classes, depending on table row's color and content). Is there a better way to write that code that expands all rows that are still collapsed?

解决方案

To match the exact class name, you could use a regex with starting and ending anchors:

$browser.div(:class => /^expander$/).exists?

This would match exactly "expander" but not "expander hasChildren" or "something expander".

For your other question regarding if there is a better way to expand all rows, I think it really depends on what the HTML for the page looks like. Do you have an example? When I had a similar problem with a similar control, I was able to use the structure of the divs around the clickable ones to make the search more specific.

这篇关于如何完全匹配使用:类选择器在watir-webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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