How do I locate a custom tag like ?

后端 未结 2 450
梦毁少年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条回答
  • There is no support for non-standard HTML tags in watir or watir-webdriver. In part because the list of possible tag names we'd have to support is endless.

    You could monkeypatch in your own custom tags if you want to. In the long run, if you are going to have to deal with those custom tags a lot, that might be your best solution in terms of having stuff that acts just like other standard HTML elements supported by the Watir api.

    You can use a CSS selector on the tag name, but that only works if you stick to element objects, as described in Watir::Exception::MissingWayOfFindingObjectException: invalid attribute: :css. Which kinda makes things a little less readable and/or useful if you ask me, but might be a quick and easy solution worth the slight cost in that regard.

    Or you can use .driver or .wd methods to access webdriver functionality when you need it for something not supported by watir. (but that's also not quite as readable)

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题