WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

data grid selected row or cell

Open cgetty12 opened this issue 6 years ago • 4 comments

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 gridRows = uiNoteGrid.FindElementByName("Data Panel").FindElementsByXPath("//Custom[starts-with(@Name, 'Row')]");

        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.

cgetty12 avatar Dec 06 '19 20:12 cgetty12

AppiumWebElement has a selected property that should help you determine which element is selected.

clintmasden avatar Dec 06 '19 21:12 clintmasden

Unfortunately, none of the cells return true for the selected property.

cgetty12 avatar Dec 09 '19 14:12 cgetty12

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.

pjcast avatar Apr 29 '20 01:04 pjcast

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).

pjcast avatar Apr 30 '20 23:04 pjcast