`
lilingjay
  • 浏览: 8519 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    Spring Cloud Finchley.SR1-Spring Cloud 手册-Spring Cloud 文档

    - **Using Multiple Keys and Key Rotation**:使用多个密钥和密钥轮换。 - **Serving Encrypted Properties**:提供加密属性服务。 - **Serving Alternative Formats**:提供替代格式服务。 - **Serving Plain ...

    GSM网优考试

    - **答案: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.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

    Adobe Boxes: Locating Object Proposals Using Object Adobes

    locating-ssh-hackers:一个 DIY 项目,在地图上实时显示失败的 ssh 登录尝试

    ssh 登录尝试监控这个 repo 基于这篇名为创建文章,并在设置环境方面有更多的细节。 我将使用 kubernetes 来运行以下部分: infludb:时间序列数据库grafana:用于在地图上可视化登录尝试登录收集器:rsyslogd 调用...

    2017年4月自考英语(一)部分试题及答案.docx

    If you have difficulty locating a particular book, please ask our librarians for _____ (assist).** - **答案:** assistance - **解析:** “assistance”作为名词作介词for的宾语。 **19. Just a few years ...

    cvpr2012_oral_clothes

    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 ...

    MyBase Desktop 6.0.2 破解正式版(官方更新至10/10/2011)

    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 ...

    selenium-2.31.0.tar.gz

    4. **Locating Elements**: Selenium 提供了多种方法来定位页面上的元素,如 XPath、CSS 选择器、ID、类名等。 5. **Assertions**: 为了验证测试结果,Selenium 提供了断言功能,确保预期的结果与实际结果相符。 6. ...

    TCL TK 语言8.5编程指导

    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 ...

    PSCAD详细教程

    #### 二十三、Locating the Problem Source (定位问题源头) - **定义**: 提供了技巧和策略来诊断模型中的错误。 - **功能**: - 快速找到问题所在。 - 提高调试效率。 #### 二十四、Tutorial Projects (教程项目)...

    爱立信Locating算法详解

    爱立信Locating算法是通信网络中用于移动设备定位的一种高级技术,主要应用于移动通信系统,如LTE或5G网络,以实现精确的用户位置跟踪和管理。此算法结合了网络基础设施、无线环境特性和服务质量需求,旨在提供高效...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    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 ...

    developing android application with adobe air.part3.rar

    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...

    GSM理论试题库

    - **解析**:Locating过程主要指在网络中选择信号质量最好的小区,以提高通信质量和效率。 #### 12. 小区负荷分担的作用 - **问题**:关于小区负荷分担的描述,哪项是错误的? - **答案**:可通过加大CRH来增大小区...

Global site tag (gtag.js) - Google Analytics