Locating a LI element using :text using watir-webdriver

原文链接: http://stackoverflow.com/questions/5948881/locating-a-li-element-using-text-using-watir-webdriver

 

Question

 

 

I am trying to locate and click on a jQuery menu element, the menu is defined as multiple UL elements containing a number of LI elements.

Using Firefox 3.6.17 on Mac 10.5, in standard WATIR I've used;

browser.li(:text,"Options...").click

or

browser.div(:id,"Attributes-menu").li(:text,"Copy").click

to click on the menu item but using watir-webdriver (0.2.3) it is reporting the LI element cannot be found. Although I can find the containing DIV and an instance of LI by using :class.

I've attached an example of a menu HTML below, can anyone suggest a reliable method of locating the LI item?

<div class="ws-menu-container ws-context-menu ws-context-menu-hidden" style="top: 16px; left: 214px; " id="Attributes-menu">
<ul class="ws-context-menu"><li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Copy&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Add...&nbsp;&nbsp;&nbsp;</li>
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Remove...&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Clear Translation Flag&nbsp;&nbsp;&nbsp;</li>
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Copy from Master Language...&nbsp;&nbsp;&nbsp;</li>
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Push to Child Languages...&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu">&nbsp;&nbsp;&nbsp;Options...&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu">&nbsp;&nbsp;&nbsp;Refresh&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu">&nbsp;&nbsp;&nbsp;Help&nbsp;&nbsp;&nbsp;</li></ul></div>

Answers
A1:

This worked for me:

browser.div(:id => "Attributes-menu").li(:text => /Copy/).click
A2:

The reason it's not found is the nbsp;'s. In addition to Željko's regexp solution, you should be able to do this by adding the Unicode bytes for a non-breaking space to the string you're looking for, e.g:

browser.li(:text => "\xc2\xa0\xc2\xa0\xc2\xa0Options...\xc2\xa0\xc2\xa0\xc2\xa0")


A3:

To do Zeljko's solution but using a string variable you need to create a new instance of the regular expression class from your string variable

searchtext = Regexp.new(mystringvariable)
browser.div(:id => "Attributes-menu").li(:text => searchtext).click

for more info see the Rdoc for the Ruby Regexp class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值