如何用Selenium点击ul里面的li标签?

时间:2017-02-14 18:53:45

标签: java selenium-webdriver

HTML:

<div class="hdetSelect right moreRooms" style="">
<select class="bookedRoom_1 perSelector xsmall number-room-booked_fbc1336de4c882dd" ref="1" id="selectRoomBooked_1" style="display: none;">
   <option value="0">0</option>
   <option value="1">1</option>
   </select>
<div class="nsSelect-container search-num-selector left">
   <input class="nsSelect-input bookedRoom_1 perSelector xsmall number-room-booked_fbc1336de4c882dd" type="text" style="display: inline-block;">
   <div class="nsSelect-opt-container search-num-selector left rounded5 hide" style="top: 696.8px; left: 875px; display: none;">
   <ul>
   <li class="clear" style="display: block; width: 27px;">
   <a href="#" ref="0" class="">0</a>
   </li>
   <li style="display: block; width: 27px;">
   <a href="#" ref="1" class="active">1</a>
   </li></ul>
   <a class="nsSelect-close">Cancel</a></div></div>
  <span class="dRoom">Room</span> 
</div>

我正在尝试使用XPath或cssSelector点击ul里面的li,但我总是得到错误“没有这样的元素:无法找到”。

以下是我正在尝试的内容:

使用cssSelector的JavaScript执行程序:

WebElement room_num = driver.findElement(By.cssSelector("div.room-list-item:nth-child(1)> div:nth-child(1)> div:nth-child(4)> div:nth-child(2)> div:nth-child(2)> ul:nth-child(1)> li:nth-child(2)>a"));
executor.executeScript("arguments[0].click();",room_num);

使用XPath:

driver.findElement(By.xpath("//*[@id='room-list']/div[1]/div[1]/div[2]/div/div/ul/li[2]/a")).click();

我想用innerHTML 1选择/点击(UI)li,但似乎没有任何效果。我的想法已经不多了所以请留下反馈或解决方案。

1 个答案:

答案 0 :(得分:0)

最后我找到了来自link

的解决方案

这是我的代码的一部分,单击li时如何解决元素不可见的异常

 WebElement room_select1= driver.findElement(By.xpath("(//input[@type='text'])[2]"));
(( JavascriptExecutor)driver).executeScript("arguments[0].click();", room_select1);
int li_size=driver.findElements(By.xpath("//a[@ref='1']")).size();
driver.findElements(By.xpath("//a[@ref='1']")).get(li_size-1).click();