Locating a LI element using :text using watir-webdriver

Alastair Montgomery picture Alastair Montgomery · May 10, 2011 · Viewed 11.8k times · Source

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>

Answer

Željko Filipin picture Željko Filipin · May 10, 2011

This worked for me:

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