2015-05-20 129 views
1

我需要在下拉框中选择一个项目。此下拉框可用作ulli项目。列表项li不是从下拉菜单中选择Selenium WebDriver

下拉列表已被认可为span元素并单击下拉按钮时,被识别为UL项显示的列表。

当使用下面的代码选择该项目时,错误消息表示在点击时不显示该weblement。

元素的innerHTML属性正确返回状态的文本,但getText()方法返回空。

oStatusLi.isDisplayed()即使在打开下拉列表框时也总是返回false。

WebElement statusUl = driver.findElement(By.xpath("//*[@id='ddlCreateStatus-" + strProjId + "_listbox']")); 
statusUl.click(); 
Thread.sleep(3000); 

List<WebElement> oStatusLis = statusUl.findElements(By.tagName("li")); 

for(WebElement oStatusLi: oStatusLis){ 

    if(oStatusLi.getAttribute("innerHTML")=="Paused") 
    { 

    oStatusLi.click(); 
    break; 
    } 
} 

感谢任何机构可以帮助我选择java代码上的列表项。

+0

你能提供'html'吗? – Saifur

+1

在当前代码中oStatusLi.click();永远不会执行。对于按值进行字符串比较,您需要使用oStatusLi.getAttribute(“innerHTML”)。equals(“Paused”)而不是==。 – skandigraun

回答

0

首先也是最重要的:将WebElement存储在内存中是不好的做法,因为它可能会导致StaleElementExceptions。它现在可能会起作用,但是在这之后,你会因为这个原因而发生奇怪的失败而头疼。其次,您可以使用单个选择器来处理元素的选择,而不是将所有的元素加载到内存中并遍历它们。

//Store the selectors rather than the elements themselves to prevent receiving a StaleElementException 
String comboSelector = "//*[@id='ddlCreateStatus-" + strProjId + "_listbox']"; 
String selectionSelector = comboSelector + "//li[contains(.,'Paused')]"; 

//Click your combo box. I would suggest using a WebDriverWait or FluentWait rather than a hard-coded Thread.sleep here 
driver.findElement(By.xpath(comboSelector)).click(); 
Thread.sleep(3000); 

//Find the element to verify it is in the DOM 
driver.findElement(By.xpath(selectionSelector));  

//Execute JavaScript function scrollIntoView on the element to verify that it is visible before clicking on it. 
JavaScriptExecutor jsExec = (JavaScriptExecutor)driver; 
jsExec.executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath(selectionSelector))); 
driver.findElement(By.xpath(selectionSelector)).click(); 

你可能只是最终不得不执行元件上的JavaScript函数点击为好,这取决于是否scrollIntoView工作。

相关问题