2014-08-27 57 views

回答

1

区别在于Watir :: Element是懒惰的位置,Watir :: ElementCollections的位置在那里。

的的Watir ::元#ole_object方法只是返回的@o值:

def ole_object 
    @o 
end 

@o只有当locate方法被称为集:

def locate 
    @o = @container.locator_for(TaggedElementLocator, @specifiers, self.class).locate 
end 

当您创建的Watir: :元素,但不要对它做任何事情,您可以看到它尚未找到(即,位于懒惰位置):

p browser.link(id: 'an id that does not need to exist') 
#~ #=> #<Watir::Link:0x..fc40fdd4a located=false specifiers={:tag_name=>["a"], :id=>"an id that does not need to exist"}> 

一旦元素被定位,通常当locate通过其他方法,如text调用,@o将设置与WIN32OLE对象:

e = browser.link 
p e.ole_object 
#=> nil 

e.locate # this is called internally by other methods such as text, click, etc. 
p e.ole_object 
#=> #<WIN32OLE:0x30de3c0> 

相反,你会看到,元素集合的方法,如links会马上去查找所有元素。因此,这些元素已经被定位并且具有指定的@o

p @browser.links(id: 'some id that exists') 
#<Watir::LinkCollection:0x58b43284 length=1 container=#<Watir::Browser:0x40fa1c8c url="https://some.page.com" title="Test Page">> 
+0

谢谢。我已经注意到了这一点,当我试图将链接传递给某个函数来模拟需要ole_object存在的硬件单击时。 – 2014-08-29 07:32:28