我需要帮助从相同的HTML代码编写Xpath

时间:2017-08-04 14:04:59

标签: selenium xpath selenium-webdriver webdriver

我写过这个

xpath(//li[contains(@class,'vdl-list__option')][28])

适用于发布日期。

但我不知道如何编写Expire Date Xpath,因为它们都有相同的ID。请帮帮我吧。

这些是我在过期日期尝试的Xpath,而Xpath只看到发布日期。

  • 这给出了2个匹配的节点:

    //div[@class='vdl-popup__content'][1]//li[contains(@class,'vdl-list__option')][28]
    
  • 这也给了2个匹配的节点。

    //li[contains(@class,'vdl-list__option')][28][1]
    //li[contains(@class,'vdl-list__option')][28]
    
<label>
    <h3>Publish Date</h3>
    <div>
    <label class="vdl-radio vdl-radio--checked">
    <div class="dateTime">
    <div>Date</div>
    <div class="vdl-date-time-picker" tabindex="-1">
    <div>Time</div>
    <div class="vdl-date-time-picker" tabindex="-1">
    <input id="vdl_20_input" class="vdl-date-time-picker__input" aria-expanded="false" aria-busy="false" aria-owns="vdl_20_time_listbox" aria-haspopup="true" role="combobox" autocomplete="off" tabindex="0" value="10:30 AM" aria-disabled="false" aria-activedescendant="vdl_20_time_listbox__option__21" type="text"/>
    <span class="vdl-date-time-picker__select">
    <div class="vdl-timelist-popup vdl-popup vdl-popup--animate" style="display: none; overflow: hidden; height: 211.6px;">
    <div class="vdl-popup__content" style="transform: translateY(-100%); position: absolute;">
    <div>
    <ul id="vdl_20_time_listbox" class="vdl-list" aria-labelledby="vdl_20_input" aria-live="false" aria-hidden="true" tabindex="-1" role="listbox">
    <li id="vdl_20_time_listbox__option__0" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">12:00 AM</li>
    <li id="vdl_20_time_listbox__option__1" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">12:30 AM</li>
    <li id="vdl_20_time_listbox__option__2" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">1:00 AM</li>
    <li id="vdl_20_time_listbox__option__3" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">1:30 AM</li>
    <li id="vdl_20_time_listbox__option__4" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">2:00 AM</li>
    <li id="vdl_20_time_listbox__option__5" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">2:30 AM</li>
    <li id="vdl_20_time_listbox__option__6" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">3:00 AM</li>
    <li id="vdl_20_time_listbox__option__7" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">3:30 AM</li>
    <li id="vdl_20_time_listbox__option__8" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">4:00 AM</li>
    <li id="vdl_20_time_listbox__option__9" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">4:30 AM</li>
    <li id="vdl_20_time_listbox__option__10" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">5:00 AM</li>
    <li id="vdl_20_time_listbox__option__11" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">5:30 AM</li>
    <li id="vdl_20_time_listbox__option__12" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">6:00 AM</li>
    <li id="vdl_20_time_listbox__option__13" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">6:30 AM</li>
    <li id="vdl_20_time_listbox__option__14" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">7:00 AM</li>
    <li id="vdl_20_time_listbox__option__15" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">7:30 AM</li>
    <li id="vdl_20_time_listbox__option__16" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">8:00 AM</li>
    <li id="vdl_20_time_listbox__option__17" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">8:30 AM</li>
    <li id="vdl_20_time_listbox__option__18" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">9:00 AM</li>
    <li id="vdl_20_time_listbox__option__19" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">9:30 AM</li>
    <li id="vdl_20_time_listbox__option__20" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">10:00 AM</li>
    <li id="vdl_20_time_listbox__option__21" class="vdl-list__option vdl-list__option--focus vdl-list__option--selected" role="option" tabindex="-1" aria-selected="true">10:30 AM</li>
    <li id="vdl_20_time_listbox__option__22" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">11:00 AM</li>
    <li id="vdl_20_time_listbox__option__23" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">11:30 AM</li>
    <li id="vdl_20_time_listbox__option__24" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">12:00 PM</li>
    <li id="vdl_20_time_listbox__option__25" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">12:30 PM</li>
    <li id="vdl_20_time_listbox__option__26" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">1:00 PM</li>
    <li id="vdl_20_time_listbox__option__27" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">1:30 PM</li>
    <li id="vdl_20_time_listbox__option__28" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">2:00 PM</li>
    <li id="vdl_20_time_listbox__option__29" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">2:30 PM</li>
    <li id="vdl_20_time_listbox__option__30" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">3:00 PM</li>
    <li id="vdl_20_time_listbox__option__31" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">3:30 PM</li>
    <li id="vdl_20_time_listbox__option__32" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">4:00 PM</li>
    <li id="vdl_20_time_listbox__option__33" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">4:30 PM</li>
    <li id="vdl_20_time_listbox__option__34" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">5:00 PM</li>
    </ul>
    </div>        

第二个HTML:

<div>
    <h3>Expiration</h3>
    <i>Defaults in 24 hours</i>
    <div class="dateTime">
    <!-- react-text: 2009 -->
    Date
    <!-- /react-text -->
    <div class="vdl-date-time-picker" tabindex="-1">
    <div>Time</div>
    <div class="vdl-date-time-picker" tabindex="-1">
    <input id="vdl_22_input" class="vdl-date-time-picker__input" aria-expanded="false" aria-busy="false" aria-owns="vdl_22_time_listbox" aria-haspopup="true" role="combobox" autocomplete="off" tabindex="0" value="" aria-activedescendant="vdl_22_time_listbox__option__0" type="text"/>
    <span class="vdl-date-time-picker__select">
    <div class="vdl-timelist-popup vdl-popup vdl-popup--animate" style="display: none; overflow: hidden; height: 211.6px;">
    <div class="vdl-popup__content" style="transform: translateY(-100%); position: absolute;">
    <div>
    <ul id="vdl_22_time_listbox" class="vdl-list" aria-labelledby="vdl_22_input" aria-live="false" aria-hidden="true" tabindex="-1" role="listbox">
    <li id="vdl_22_time_listbox__option__0" class="vdl-list__option vdl-list__option--focus" role="option" tabindex="-1" aria-selected="false">12:00 AM</li>
    <li id="vdl_22_time_listbox__option__1" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">12:30 AM</li>
    <li id="vdl_22_time_listbox__option__2" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">1:00 AM</li>
    <li id="vdl_22_time_listbox__option__3" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">1:30 AM</li>
    <li id="vdl_22_time_listbox__option__4" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">2:00 AM</li>
    <li id="vdl_22_time_listbox__option__5" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">2:30 AM</li>
    <li id="vdl_22_time_listbox__option__6" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">3:00 AM</li>
    <li id="vdl_22_time_listbox__option__7" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">3:30 AM</li>
    <li id="vdl_22_time_listbox__option__8" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">4:00 AM</li>
    <li id="vdl_22_time_listbox__option__9" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">4:30 AM</li>
    <li id="vdl_22_time_listbox__option__10" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">5:00 AM</li>
    <li id="vdl_22_time_listbox__option__11" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">5:30 AM</li>
    <li id="vdl_22_time_listbox__option__12" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">6:00 AM</li>
    <li id="vdl_22_time_listbox__option__13" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">6:30 AM</li>
    <li id="vdl_22_time_listbox__option__14" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">7:00 AM</li>
    <li id="vdl_22_time_listbox__option__15" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">7:30 AM</li>
    <li id="vdl_22_time_listbox__option__16" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">8:00 AM</li>
    <li id="vdl_22_time_listbox__option__17" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">8:30 AM</li>
    <li id="vdl_22_time_listbox__option__18" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">9:00 AM</li>
    <li id="vdl_22_time_listbox__option__19" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">9:30 AM</li>
    <li id="vdl_22_time_listbox__option__20" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">10:00 AM</li>
    <li id="vdl_22_time_listbox__option__21" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">10:30 AM</li>
    <li id="vdl_22_time_listbox__option__22" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">11:00 AM</li>
    <li id="vdl_22_time_listbox__option__23" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">11:30 AM</li>
    <li id="vdl_22_time_listbox__option__24" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">12:00 PM</li>
    <li id="vdl_22_time_listbox__option__25" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">12:30 PM</li>
    <li id="vdl_22_time_listbox__option__26" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">1:00 PM</li>
    <li id="vdl_22_time_listbox__option__27" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">1:30 PM</li>
    <li id="vdl_22_time_listbox__option__28" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">2:00 PM</li>
    <li id="vdl_22_time_listbox__option__29" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">2:30 PM</li>
    <li id="vdl_22_time_listbox__option__30" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">3:00 PM</li>
    <li id="vdl_22_time_listbox__option__31" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">3:30 PM</li>
    <li id="vdl_22_time_listbox__option__32" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">4:00 PM</li>
    <li id="vdl_20_time_listbox__option__33" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">4:30 PM</li>
    <li id="vdl_20_time_listbox__option__34" class="vdl-list__option" role="option" tabindex="-1" aria-selected="false">5:00 PM</li>
    </ul>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

发布日期

//h3[text()='Publish Date']/..//li[@id='vdl_22_time_listbox__option__0']
到期日

//h3[text()='Expiration']/..//li[@id='vdl_22_time_listbox__option__0']

这里的想法是找到具有相应文本的节点h3,然后转到它的父节点,然后相对于此找到您感兴趣的节点。

答案 1 :(得分:0)

我知道您接受了答案,但我不会使用ID来查找这些元素,因为如果页面因为编号而改变,将来可能会发生变化。找到它们同样容易(如果不是更容易)的方法是

//h3[.='Publish Date']/following-sibling::div//ul
//h3[.='Expiration']/following-sibling::div//ul

这也抓住了UL(所有列表项的父级),这可能比第一个LI更有用。