2012-08-02 45 views
0

我試圖點擊頂部菜單上的所有鏈接在現場http://watirwebdriver.com/如何點擊菜單中的所有li元素?

require 'watir-webdriver' 

class Proba 
    def test 
    b = Watir::Browser.new 
    b.goto "watirwebdriver.com" 
    b.ul(:id => 'menu-watirwebdriver-menu').lis.each do |li| 
     li.click 
    end 
    end 
end 

proba = Proba.new 
proba.test 

但有提出一個問題: https://gist.github.com/3239338

可能是什麼問題?

+2

注意,點擊LIS實際上不會做任何事情。如果你想點擊鏈接導航的位置,你需要點擊裏面的鏈接。 – 2012-08-02 19:22:43

回答

3

您的所有li都不可見。檢查他們是否。

要點擊僅對可見裏,你可以這樣做:

def test 
    b = Watir::Browser.new 
    b.goto "watirwebdriver.com" 
    b.ul(:id => 'menu-watirwebdriver-menu').lis.each do |li| 
     li.click if li.visible? 
    end 
    end 
+0

這是不可見的子菜單。如果只有最高級別的lis是想要的,那麼最好只得到那些--b.ul(:id =>'menu-watirwebdriver-menu')。lis(:xpath,'li')。each'。那麼你不需要進行可視性檢查。 – 2012-08-02 19:21:28

相關問題