watir-webdriver - 如何找到<g>之类的自定义标签?

标签 watir-webdriver

我必须处理页面中带有<g>标记的某些图形项。我可以执行以下操作来找到它们,方法是放入Selenium Webdriver:

browser.wd.find_elements( :tag_name => "g" )

watir网络驱动程序等效于什么?

另外,我该如何将这些 Selenium 元素转换为沃特尔元素呢?

我可以以某种方式在本地watir中添加对<g>标记的支持吗?

最佳答案

解决方案1-与Watir-Webdriver等效:

与您在selenium-webdriver中所做的等效:

browser.elements( :tag_name => "g" )

因此,您可以执行以下操作以输出每个元素的文本:
browser.elements( :tag_name => "g" ).each do |x|
  puts g.text
end

解决方案2-添加对G元素的支持:

要求watir-webdriver后,添加以下代码:
module Watir
    module Container
        def g(*args)
            G.new(self, extract_selector(args).merge(:tag_name => "g"))
        end

        def gs(*args)
            GCollection.new(self, extract_selector(args).merge(:tag_name => "g"))
        end         
    end

    class G < Element
    end

    class GCollection < ElementCollection
        def element_class
            G
        end
    end
end

然后,您可以像对待其他任何元素一样对待'g'。例如:
puts browser.g(:index, 0).text
browser.gs.each{ |x| puts x.text }

G和GCollection类将支持所有标准元素方法。如果有特定于该元素的内容,则可以向该类添加其他方法。

更新-添加自定义方法的示例:

要获得游标样式,您可以向G类添加一个方法,如下所示:
class G < Element
    def cursor_style()
        assert_exists
        return @element.style("cursor")
    end
end

然后,这将允许您获得如下游标属性:
puts browser.g(:index, 0).cursor_style
#=> move

任何与元素交互的自定义​​方法都必须以assert_exists开头。然后,在方法中,您可以使用@element变量来处理元素。

请注意,由于G元素是从Element类继承的,因此您还可以使用内置的样式方法:
puts browser.g(:index, 0).style("cursor")
#=> move

关于watir-webdriver - 如何找到<g>之类的自定义标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10713102/

相关文章:

css - Watir-webdriver 无法在弹出窗口中看到输入标签

ruby - 使用 watir-webdriver 的首屏截图

selenium - 类型错误 : Failed to execute 'createNSResolver' on 'Document' : parameter 1 is not of type 'Node'

internet-explorer - Watir Webdriver 文本在 IE 上输入缓慢

ruby - 找出 Firefox 通过 watir-webdriver 保存的文件名

cucumber - Selenium Webdriver 错误代码 502

ruby - 在 ubuntu 11.10 中安装服务员时出错

cucumber - 为众多客户提供 Watir-Webdriver 现场覆盖的方法

ruby - 如何在 Cucumber 步骤定义中使单词可选?

watir - 如何在访问 Watir 网页之前加载 cookie