2014-12-28 30 views
1

我需要在monthpicker中選擇月份。 的XPath具有隨機數如何點擊在xpath或css路徑中有隨機數的元素?

//*[@id="monthpicker_037945738616321245"]/table/tbody/tr[4]/td[3] 

CSS路徑具有隨機數過多

#monthpicker_037945738616321245 > table > tbody > tr:nth-child(4) > td:nth-child(3) 

另外3頁蛾採摘,但只有一個是可見的。 包含此monthpicker的每個div都具有包含隨機數的id。 monthpicker_04289214732160275monthpicker_04395144395656033

<table class="mtz-monthpicker"> 
    <tbody class="mtz-monthpicker"> 
     <tr class="mtz-monthpicker"> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="1">Январь</td> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="2">Февраль</td> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="3">Март</td> 
     </tr> 
     <tr class="mtz-monthpicker"> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="4">Апрель</td> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="5">Май</td> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="6">Июнь</td> 
     </tr> 
     <tr class="mtz-monthpicker"> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="7">Июль</td> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="8">Август</td> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="9">Сентябрь</td> 
     </tr> 
     <tr class="mtz-monthpicker"> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="10">Октябрь</td> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="11">Ноябрь</td> 
     <td class="ui-state-default mtz-monthpicker mtz-monthpicker-month" style="padding:5px;cursor:default;" data-month="12">Декабрь</td> 
     </tr> 
    </tbody> 
</table> 

我試圖

@b.table(:class => 'mtz-monthpicker').tr(:class => 'mtz-monthpicker').td(:class => 'ui-state-default mtz-monthpicker mtz-monthpicker-month').click 

CSS路徑:#monthpicker_020622412423150185 > table > tbody > tr:nth-child(4) > td:nth-child(3)

的xpath://*[@id="monthpicker_020622412423150185"]/table/tbody/tr[4]/td[3]

另外angular.is用於該前端。

如何在本月採購員中選擇並點擊一個月?

回答

2

一個可能的解決方案是隻匹配id值的一部分,因此您不需要對隨機數進行硬編碼。這可以通過使用XPath函數或starts-with()來完成:

//*[contains(@id, "monthpicker_")]/..... 
//*[starts-with(@id, "monthpicker_")]/..... 
1

在這種情況下,我會去的基於文本的搜索xpath。這是我最喜歡的。

//*[.='Март'] //if the text is unique 

隨着.我們可以直接過濾到父節點,尋找匹配的文本。但是,您提到的UI基於Angular。我不確定Watir如何處理與webdriver效果不佳的角度,因此您可能需要在此之前添加一些額外的等待時間。

+0

我使用watir-webdrievr。我試過:@ b.td(:xpath,「// * [contains(@id,'monthpicker _')]/table/tbody/tr [4]/td [3]」)。when_present.click @b。 td(:xpath,「// * [starts-with(@id,'monthpicker _')]/table/tbody/tr [4]/td [3]」)。when_present.click @ b.td(:xpath ,「//*[.='Март']").when_present.click但是我在90秒後才超時,等待{:xpath => .... – user1371940

+0

之後發生了什麼? – Saifur

+0

這個xpath可以工作,但是這個元素目前不可見,硒不能與它交互。此外這個元素沒有任何可以隱藏的樣式。但它有父親溢出:隱藏的風格。我嘗試用javascript代碼更改父元素的可見性:monthPikerOverflow = @ b.div(:xpath,「// * [contains(@id,'monthpicker_')]」) script = << - JS arguments [0] .style.overflow ='visible'; arguments [0] .style.visibility ='visible'; JS @ b.execute_script(script,monthPikerOverflow),但它沒有幫助。所以我一直在尋找解決方案。 – user1371940

0

鑑於您還需要檢查可見性,我不認爲您只能使用XPath或CSS。

要找到可見的monthpicker,請獲取id中包含「monthpicker」的所有div。就個人而言,我發現使用正則表達式比一個XPath/CSS更容易:

monthpicker = @b.divs(id: /monthpicker_/).find(&present?) 

然後你可以用monthpicker互動點擊其中一個細胞。例如,單擊第一個單元格:

monthpicker.td.when_present.click 

或點擊一個特定的指標(第三個在這種情況下):雖然我覺得這可能是更清晰的選擇基礎上,TD

monthpicker.td(index: 2).when_present.click 

其數據月屬性:

monthpicker.td(data_month: "3").when_present.click 
0

謝謝大家的建議。

monthpicker = @b.tds(:xpath, "//*[contains(@id, 'monthpicker_')]/table/tbody/tr[4]/td[3]") 
puts monthpicker.size 

導致4個元素

這爲我工作

monthpicker.each do |x| 
script = <<-JS 
arguments[0].click(); 
JS 
@b.execute_script(script, x) 
end 

如果我使用x.click然後我會得到「元素當前不可見」,因爲我猜有些父元素有溢出:隱藏屬性。但JS的作品。

相關問題