如何找到像 <g> 这样的自定义标签? [英] How do I locate a custom tag like <g>?
问题描述
我必须处理页面中具有 <g>
标签的一些图形项目.我可以通过插入 selenium webdriver 来做这样的事情来找到它们:
I'm having to deal with some graphic items in a page that have <g>
tags. I can do something like this to find them by dropping into selenium webdriver:
browser.wd.find_elements( :tag_name => "g" )
watir webdriver 的等价物是什么?
What would be the equivalent for watir webdriver?
另外,我如何将这些硒元素转换为 watir 元素?
Also, how would I convert those selenium elements into watir elements?
我能否以某种方式添加对 <g>
标签的支持以在本地使用?
Could I add support for a <g>
tag to watir locally somehow?
推荐答案
解决方案 1 - Watir-Webdriver 等效:
与您在 selenium-webdriver 中所做的等效的是:
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
解决方案 2 - 添加对 G 元素的支持:
在需要 watir-webdriver 后,添加以下代码:
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
然后你可以像对待任何其他元素一样对待 'g'.例如:
Then you can treat 'g' like any other element. For example:
puts browser.g(:index, 0).text
browser.gs.each{ |x| puts x.text }
G 和 GCollection 类将支持所有标准元素方法.如果有特定于该元素的内容,您可以向该类添加其他方法.
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.
更新 - 添加自定义方法的示例:
要获得光标样式,您需要向 G 类添加一个方法,如下所示:
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
任何与元素交互的自定义方法都需要以 assert_exists 开头.然后,在该方法中,您可以使用 @element 变量处理元素.
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.
请注意,由于 G 元素继承自 Element 类,因此您也可以使用内置样式方法:
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
这篇关于如何找到像 <g> 这样的自定义标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!