Reputation: 21
The dropdown
is inside the table.
Here is my HTML CODE:
<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>
My selenium code to locate and select dropdown inside the table is:
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']")));
...
}
}
It get a NoSuchElementException
on
By.xpath(".//*[@id='stockLoc_66']")
Upvotes: 0
Views: 364
Reputation: 193338
The iterations through different rows
and columns
looks as a overhead to me. Though @JeffC 's Answer looks pretty perfect but we may require to cover some more boundary conditions. @JeffC in his Answer have used visibilityOfElementLocated
clause as ExpectedConditions
which will handle the expectations while checking that an element is present on the DOM of a page and visible.
However I will get a bit more granular and use elementToBeSelected
clause as ExpectedConditions
which will handle the expectations while checking if the given element is selected as follows:
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");
Upvotes: 0
Reputation: 13722
The reason is only the 7th cell of each tbody row includes the select, so when the loop iterate on the first cell, there is no select inside it, so the wait report NoSuchElement exception.
Upvotes: 0
Reputation: 25714
There's a lot of missing information that would help narrow down an answer but for starters, you can simplify your code which might help narrow down the problem. You don't need all the looping through rows and cells to find the SELECT
. The code below should get the element just fine.
Select stockLocation = new Select(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("stockLoc_66"))));
stockLocation.selectByVisibleText(...);
If that doesn't work. Make sure it's not in an iframe, make sure there's not more than one element with id='stockLoc_66'
, etc.
Don't use XPath when you are locating an element using only an ID. There's no need. CSS Selectors and IDs are better supported and faster than XPath.
Upvotes: 0
Reputation: 4749
Try with name
driver.findElement(By.name("stockLoc_66"));
or try xpath
driver.findElement(By.xpath(".//select[@id='stockLoc_66']"));
Upvotes: 0