简体   繁体   English

Watir获得INNER的HTML

[英]Watir get INNER html of <span>

I'm trying to find a navigation link by iterating through a handful of spans with class 'menu-item-text'. 我正在尝试通过对类'menu-item-text'的跨度进行迭代来找到导航链接。 My goal is to compare what is inside the span tags to see if it is the right navigation control to click (there are no hard ids to go by.) My code is like this: 我的目标是比较span标记内的内容,以查看它是否是单击的正确导航控件(没有任何硬ID。)我的代码是这样的:

navlinks = @browser.spans(:class, 'menu-item')
navlinks.each do |this|
    puts "'#{this.text}'"
    if this.text == link_name
        this.click
    break
end

I know for sure I'm getting the correct elements. 我肯定知道我得到了正确的元素。 However, text is always an empty string. 但是,文本始终是一个空字符串。 My second idea was to use .html instead of .text, but that returns something like this: 我的第二个想法是使用.html而不是.text,但是返回的内容如下:

<span class="menu-item">Insights</span>

What I want is the "Insights" text inside the span, not the full html that includes the tag markup. 我想要的是跨度内的“ Insights”文本,而不是包含标签标记的完整html。 I have also tried using this.span.text, but that did not work either. 我也尝试过使用this.span.text,但这也不起作用。

How can I target exclusively the inner html of an element through watir's content grabbing methods? 如何通过watir的内容获取方法专门定位元素的内部html?

Thanks! 谢谢!

Assuming you are using Watir-Webdriver v0.6.9 or later, a inner_html method has been added for getting the inner HTML. 假设您使用的是Watir-Webdriver v0.6.9或更高版本,则添加了inner_html方法来获取内部HTML。

For the span: 对于跨度:

<span class="menu-item">Insights</span>

You could do: 您可以这样做:

@browser.span(:class => 'menu-item').inner_html
#=> "Insights"

Similarly, you could try using this method in your loop instead of .text . 同样,您可以尝试在循环中使用此方法,而不是.text

Note that depending on the uniqueness of your text, you might be able to simply check if the text appears in element's (outer) html: 请注意,根据文本的唯一性,您也许可以简单地检查文本是否出现在元素的(外部)html中:

@browser.spans(:class => 'menu-item', :html => /#{link_name}/).click

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM