2017-10-11 89 views
1

我有如下畫面: enter image description here 我使用下面的Ruby腳本點擊「新增」按鈕:不能點擊同一元素類使用的Watir

vendorTab = driver.a id: "tab-master-tab-vendor" 
    vendorTab.wait_until_present 
    if vendorTab.exists? 
    vendorTab.click 
    end 

    addNewButton = driver.button class: ['btn btn-primary'] 
    addNewButton.wait_until_present 
    if addNewButton.exists? 
     addNewButton.click 
    end 

但是,當我移動到另一個選項卡,並嘗試點擊相同的「添加新的」按鈕,Ruby腳本不起作用。 enter image description here 我的Ruby代碼有什麼問題嗎?

buildingTypeTab = driver.a id: "tab-master-tab-building" 
buildingTypeTab.wait_until_present 
if buildingTypeTab.exists? 
    buildingTypeTab.click 
end 

    addNewButton = driver.button class: ['btn btn-primary'] 
    addNewButton.wait_until_present 
    if addNewButton.exists? 
     addNewButton.click 
    end 

我感謝您的幫助。非常感謝你。

回答

3

我想所有這些標籤是同一網頁的一部分?也就是說,都在同一個HTML中?

如果是這樣的話,driver.button class: ['btn btn-primary']會阻止時,發現在HTML中的第一個實例,但是這不是你正在尋找每一次的按鈕(這是在第一個選項卡,在這裏你的腳本按鈕按照您的預期工作)。

在我心中最好的選擇是

  • 找到一種方法來唯一識別每個選項卡中的按鈕(例如,使用id代替class如果可能的話),或
  • 把所有的按鈕進入在找出哪個索引與每個選項卡對齊後,使用其集合索引單擊該按鈕。例如,

button_collection = browser.buttons(:class, ['btn', 'btn-primary']) button_collection[2].click # Will click the 3rd button in the collection

+0

感謝您的建議。我現在正在工作... – FMD

0

閱讀從PJD的建議後, 我修改了一下,得到它的工作是這樣的:

buildingTypeTab = driver.a id: "tab-master-tab-building" 
buildingTypeTab.wait_until_present 
if buildingTypeTab.exists? 
    buildingTypeTab.click 
end 

    addNewButton = driver.button(:class => ['btn btn-primary'], :index => 2) 
    addNewButton.wait_until_present 
    if addNewButton.exists? 
     addNewButton.click 
    end 

正如PJD說,是這些標籤是同一個HTML的一部分

謝謝。

+0

如果有人回答你的問題,請接受他們的答案,他們會得到15分,你需要點擊他們答案左上角的勾號符號。 – RAJ

相關問題