2017-09-13 23 views
0

dropdown在表格内。NOSUCHELEMENTEXCEPTION:无法在表格内找到下拉列表

这里是我的html代码:

<div class="table-responsive"> 
    <table class="table notification-table"> 
    <thead> 
     <tr> 
     <th>ITEM</th> 
     <th>PRODUCT CODE</th> 
     <th>WEIGHT</th> 
     <th>AVAIL</th> 
     <th>LOT NUMBER</th> 
     <th>STORE QTY</th> 
     <th>STORAGE LOC</th> 
     <th>STORAGE POINT</th> 
     <th/> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
     <td>Chicken</td> 
     <td>AG0000</td> 
     <td>2Kg</td> 
     <td>5</td> 
     <td>57</td> 
     <td>5</td> 
     <td> 
      <div class="styled-select slate quantity"> 
      <select id="stockLoc_66" class="cursor stockLocationListItem stockSelectItem selected" name="stockLoc_66"> 
       <option value="">Select One</option> 
       <option value="Loc1">Loc1</option> 
       <option value="Loc2">Loc2</option> 
       <option value="Loc3">Loc3</option> 
       <option value="hgfhf">hgfhf</option> 
       <option value="123">123</option> 
       <option value="Loc 7">Loc 7</option> 
      </select> 
      </div> 
     </td> 
     <td> 
      <td> 
     </tr> 
    </tbody> 
    </table> 
</div> 

我的硒代码,找到并选择下拉菜单里面的表是:

WebElement allocationtable = driver.findElement(By.xpath(".//table[@class='table notification-table']")); 
List<WebElement> rows = allocationtable.findElements(By.tagName("tr")); 
for (WebElement eachrow : rows) 
{ 
    List<WebElement> columns = eachrow.findElements(By.tagName("td")); 
    for (WebElement eachcolumn : columns) 
    { 
     String elementtext = "STORAGE LOC"; 
     String elementtext2 = "STORAGE POINT"; 
     WebDriverWait wait = new WebDriverWait(driver, 20); 
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='stockLoc_66']"))); 
     Select storagelocation = new Select(driver.findElement(By.xpath(".//*[@id='stockLoc_66']"))); 
     ... 
    } 
} 

它得到一个NoSuchElementException

By.xpath(".//*[@id='stockLoc_66']") 
+1

我不明白为什么要使用XPath当你可以直接使用By.id()。 – SaiPawan

+0

请发布一个链接到页面,以便我们可以看到完整的HTML。在这里你还没有描述的其他事情正在发生。哪一行有问题?你已经指出了一个位于多行的定位器。同时发布完整的异常消息。你有没有检查iframe等? – JeffC

+0

页面上是否有多个元素与'id =“stockLoc_66”'? – JeffC

回答

0

的原因是每个tbody行中只有第7个单元格包含select,因此当循环迭代到第一个时cell,里面没有select,所以等待报告NoSuchElement异常。

+0

他用来查找select的定位符只是一个ID,它在HTML中的位置并不重要。他的循环遍历行和单元格的代码不会改变他使用的定位器,因此搜索与他所在的位置无关。 – JeffC

0

尝试用名称

driver.findElement(By.name("stockLoc_66")); 

,或者尝试XPath的

driver.findElement(By.xpath(".//select[@id='stockLoc_66']")); 
+0

也会出现同样的问题。它也不会被'name'找到 –

+0

也可以用类名尝试。你可以分享网址吗? – iamsankalp89

0

有很多缺失,这将有助于缩小一个答案,但对于初学者来说,可以简化您的代码,这可能有助于缩小信息解决问题。您不需要遍历行和单元格来查找SELECT。下面的代码应该得到的元素就好了。

Select stockLocation = new Select(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("stockLoc_66")))); 
stockLocation.selectByVisibleText(...); 

如果这不起作用。确保它不在iframe中,确保id='stockLoc_66'等不多于一个元素等。

当您仅使用ID定位元素时,不要使用XPath。没有必要。 CSS选择器和ID更好地支持并且比XPath更快。

0

通过不同rowscolumns的迭代看起来对我来说是一种开销。尽管@JeffC的答案看起来很完美,但我们可能需要覆盖更多的边界条件。 @JeffC在他的答案中使用了visibilityOfElementLocated子句作为ExpectedConditions,它将处理期望,同时检查元素是否出现在页面的DOM上并且可见。

但是我会变得有点更精细,并使用elementToBeSelected子句ExpectedConditions它将处理预期中检查是否选择了给定的元素,同时如下:

WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.elementToBeSelected(By.id("stockLoc_66"))); 
Select storagelocation = new Select(driver.findElement(By.id("stockLoc_66"))); 
storagelocation.selectByValue("Loc1");