data grid selected row or cell
I am able to traverse through a data grid using WinAppDriver. However, I cannot tell which of the rows/cells is selected.
This is how I get to the cell.
ReadOnlyCollection
foreach (AppiumWebElement row in gridRows)
{
ReadOnlyCollection<AppiumWebElement> cells = row.FindElementsByXPath("//DataItem[contains(@Name, 'row')]");
foreach (AppiumWebElement cell in cells)
{
// work with cells here
}
}
I tried to use the attribute LegacyState, but that did not prove successful.
AppiumWebElement has a selected property that should help you determine which element is selected.
Unfortunately, none of the cells return true for the selected property.
Kind of late, perhaps you already found a solution? If so, would love to hear it. The one method I came across, kind of a kludge, is to click/focus the grid - to ensure the active row has focus, then find the parent (if on a cell editor): some partial python snippet:
ui is a WebElement
def selected_row(self):
self.ui.click()
cur_focus = self.object.driver.switch_to.active_element
if self._isDataRow(cur_focus) == False:
xpath = '//*[@RuntimeId="{0}"]/parent::*'.format(cur_focus.id)
cur_focus = self.object.driver.find_element_by_xpath(xpath)
if self._isDataRow(cur_focus) == False:
raise ValueError("Could not find selected row")
return cur_focus
def _isDataRow(self, item):
if item == None:
raise ValueError("Unexpected Null Element while looking for selected")
tag = item.tag_name
if tag == "DataItem":
return True
elif tag == "ControlType.Custom" and item.get_attribute("Name").startswith("Row "):
return True
return False
I have not tested all kinds of scenarios (readonly grid/cells/different editors/etc), have been looking for alternative ways... but, looked to work. I don't quite like having to change focus to determine if something is selected.
Looks like, actually, sending SHIFT to the grid control (self.ui.send_keys(Keys.SHIFT)) is best - instead of the click(). As it gives it focus, but doesn't change the selected row (my initial grid test had narrow columns, so center of grid was gray, and didn't change the rows).