How do I locate a custom tag like ?

后端 未结 2 468
梦毁少年i
梦毁少年i 2021-01-03 04:32

I\'m having to deal with some graphic items in a page that have tags. I can do something like this to find them by dropping into selenium webdriver:<

2条回答
  •  梦谈多话
    2021-01-03 05:19

    Solution 1 - Watir-Webdriver Equivalent:

    The equivalent to what you were doing in selenium-webdriver is:

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

    So you can do something like this to output the text of each element:

    browser.elements( :tag_name => "g" ).each do |x|
      puts g.text
    end
    

    Solution 2 - Add Support for G Element:

    After requiring watir-webdriver, add the following code:

    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
    

    Then you can treat 'g' like any other element. For example:

    puts browser.g(:index, 0).text
    browser.gs.each{ |x| puts x.text }
    

    The G and GCollection classes will support all the standard element methods. You can add additional methods to the class if there are things specific to that element.

    Update - Example of Adding Custom Methods:

    To get the cursor style, you would add a method to the G class like this:

    class G < Element
        def cursor_style()
            assert_exists
            return @element.style("cursor")
        end
    end
    

    This will then allow you to get the cursor property like this:

    puts browser.g(:index, 0).cursor_style
    #=> move
    

    Any custom methods that interact with the element need to start with assert_exists. Then, within the method, you can work with the element using the @element variable.

    Note that because the G element inherits from the Element class, you could also have used the built in style method:

    puts browser.g(:index, 0).style("cursor")
    #=> move
    

提交回复
热议问题