2017-02-14 32 views
0

HTML:如何在硒中點擊ul裏面的li標籤?

<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內的利,但我總是得到錯誤「沒有這樣的元素:無法找到」。

這裏就是我與努力:

使用JavaScript執行與cssSelector

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(); 

我要選擇/點擊(UI)裏有innerHTML 1,但似乎沒有任何工作。我正在用盡想法,所以請留下意見或解決方案。

+0

首先,我會建議你使用Chrome瀏覽器開發工具,你可以右鍵單擊一個元素並copy-> xpath以使用路徑的值 –

+0

是的,我的xpath選擇器是從chrome開發工具複製的。 –

+0

比我能想到的唯一原因是dom是在您嘗試訪問元素後生成的,因爲我需要更多關於涉及哪些技術的詳細信息e.t.c –

回答

0

終於讓我找到解決方案從link

這是我的代碼部分,如何解決元素不可見異常,當點擊李

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();