原文链接: 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"> Copy </li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu-disable"> Add... </li>
<li class="ws-context-menu-disable"> Remove... </li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu-disable"> Clear Translation Flag </li>
<li class="ws-context-menu-disable"> Copy from Master Language... </li>
<li class="ws-context-menu-disable"> Push to Child Languages... </li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu"> Options... </li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu"> Refresh </li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu"> Help </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
发表评论
-
Get values from a select list using xpath [closed]
2012-04-04 16:50 0http://stackoverflow.com/que ... -
How do I build Watir tests for websites?
2012-04-04 16:14 0http://stackoverflow.com/que ... -
“TypeError: can't convert String into Integer” when trying to CTRL+click or SHIF
2012-04-04 15:33 0http://stackoverflow.com/que ... -
Using Watir, how can I select a radio control using both name and value?
2012-04-04 15:23 0q http://stackoverflow.com/q ... -
Selecting multiple options in select list with shift
2012-04-04 15:19 0Q http://stackoverflow.com/ques ... -
Drag-and-Drop in Watir
2012-04-04 15:15 0http://www.cheezyworld.com/2010 ... -
How to test onclick in this object without id
2012-04-04 15:13 0http://stackoverflow.com/que ... -
Browser Certificates
2012-04-04 15:01 0http://watirwebdriver.com/b ... -
selected_options returns blank from iframe
2012-04-04 14:48 0link http://stackoverflow.com/q ... -
How to handle tinyMCE when automating with watir-webdriver?
2012-04-04 14:28 0I'm evaluating Watir-webdr ... -
Browser url not returning new url
2012-04-01 17:55 0Qhttp://stackoverflow.com/quest ... -
Can not drive Opera with watir-webdriver on Windows XP
2012-04-01 17:53 0Qhttp://stackoverflow.com/quest ... -
why must I sleep or wait using watir-webdriver and firefox?
2012-04-01 17:50 0Qhttp://stackoverflow.com/quest ... -
Firefox 4 with watir webdriver: Need help using helperApps.neverAsk to save CSV
2012-04-01 17:39 0Q http://stackoverflow.com/q ... -
Watir Webdriver counting number of items in a UL list
2012-04-01 17:22 0Q http://stackoverflow.co ... -
finding the direct child of a tag in watir
2012-04-01 17:13 0Qhttp://stackoverflow.com/quest ... -
How to determine browser type (IE, FF, Chrome, etc.)
2012-04-01 16:59 0Q http://stackoverflow.com/que ... -
Wait methods for watir-webdriver unsuccessful with JS/AJAX and new page load (Er
2012-04-01 16:19 0http://stackoverflow.com/questi ... -
How to send text to a CKEditor WYSIWYG editor box using Watir-WebDriver
2012-04-01 16:14 0Q http://stackoverflow.com/ques ... -
How can I get Browser.text.include? to be case insensitive?
2012-04-01 15:57 0Q:http://stackoverflow.com/ques ...
相关推荐
- **Using Multiple Keys and Key Rotation**:使用多个密钥和密钥轮换。 - **Serving Encrypted Properties**:提供加密属性服务。 - **Serving Alternative Formats**:提供替代格式服务。 - **Serving Plain ...
- **答案:A** - **选项解析:** - A: VLR (Visitor Location Register) 访问位置寄存器 - B: HLR (Home Location Register) 主叫位置寄存器 - C: AUC (Authentication Center) 鉴权中心 - D: EIR (Equipment ...
Selenium.WebDriver.4.0.0-alpha02.nupkg,Selenium.WebDriver 4.0,Selenium is a set of different software tools each with a different approach to supporting browser automation. These tools are highly ...
Adobe Boxes: Locating Object Proposals Using Object Adobes
ssh 登录尝试监控这个 repo 基于这篇名为创建文章,并在设置环境方面有更多的细节。 我将使用 kubernetes 来运行以下部分: infludb:时间序列数据库grafana:用于在地图上可视化登录尝试登录收集器:rsyslogd 调用...
If you have difficulty locating a particular book, please ask our librarians for _____ (assist).** - **答案:** assistance - **解析:** “assistance”作为名词作介词for的宾语。 **19. Just a few years ...
by locating 30 human parts detected by a well trained human detector. Then, founded on part features, we propose a two-step calculation to obtain more reliable one-to-many similarities between the ...
1.Fixed: a bug in bookmark locating. 2.Fixed: a bug in the rtf-to-text parser, which applies to search, word count, index, etc. 3.Fixed: a bug in the toolbar, size corruption on the WM_SETTINGCHANGE ...
4. **Locating Elements**: Selenium 提供了多种方法来定位页面上的元素,如 XPath、CSS 选择器、ID、类名等。 5. **Assertions**: 为了验证测试结果,Selenium 提供了断言功能,确保预期的结果与实际结果相符。 6. ...
Locating a pattern within a string 58 Returning a range of characters from a string 59 Creating a string of repeated characters 60 Replacing ranges of characters contained within a string 60 Creating ...
#### 二十三、Locating the Problem Source (定位问题源头) - **定义**: 提供了技巧和策略来诊断模型中的错误。 - **功能**: - 快速找到问题所在。 - 提高调试效率。 #### 二十四、Tutorial Projects (教程项目)...
爱立信Locating算法是通信网络中用于移动设备定位的一种高级技术,主要应用于移动通信系统,如LTE或5G网络,以实现精确的用户位置跟踪和管理。此算法结合了网络基础设施、无线环境特性和服务质量需求,旨在提供高效...
Using Special Fonts for Text 310 Displaying Images 318 Chapter 8: Event Handling 323 Basics of Event Handling 324 Actions 342 Mouse Events 349 The AWT Event Hierarchy 357 Chapter 9: User ...
Locating a Device Using Global Positioning System and Network/WiFi Technology AIR and Android Reverse Geocoding Maps EXIF Data and the Map Object The speed Property Conclusion Chapter 11 : Microphone...
- **解析**:Locating过程主要指在网络中选择信号质量最好的小区,以提高通信质量和效率。 #### 12. 小区负荷分担的作用 - **问题**:关于小区负荷分担的描述,哪项是错误的? - **答案**:可通过加大CRH来增大小区...