Skip to main content
 首页 » 编程设计

angularjs之如何单击xpath或css路径中具有随机数的元素

2025年05月04日120wuhuacong

我需要选择月选择器中的月份。
xpath具有随机数

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


CSS路径也有随机数

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


页面上还有3个防虫器,但只有一个是可见的。
包含该月选择器的每个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.。

如何选择并单击此月选择器中的一个月?

请您参考如下方法:

一种可能的解决方案是仅匹配部分id值,因此您无需对随机数进行硬编码。这可以使用XPath函数contains()starts-with()来完成:

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