dotnet-client icon indicating copy to clipboard operation
dotnet-client copied to clipboard

"Nested" FindElementsBy returns WebElements instead of WindowsElements

Open caulfield2 opened this issue 8 years ago • 6 comments

Description

When using a nested search on a Windows application using WinAppDriver, a WebElement is returned instead of a WindowsElement.

Environment

  • Appium version (or git revision) that exhibits the issue: Appium.WebDriver NuGet 3.0.0.3
  • Desktop OS/version used to run Appium: Windows 7

Using WinAppDriver.exe with C# in VS 2015 to run tests on a Windows application in a Windows 10 VM.

Details

My only experience with Appium is through using WinAppDriver to automate Windows applications, so I apologize in advance for any incorrect assumptions I make. I was recommended to post this here after originally posting to the WinAppDriver page here.

I am trying to narrow down a search to a certain section of my app and then find all elements within that section by a certain name. In VS 2015, my initial thought was to do the following:

driver = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUri), desktopCapabilities); WindowsElement element = driver.FindElementByName("name1"); IList<WindowsElement> list = (IList<WindowsElement>)element.FindElementsByName("name2");

I do not get any complaints in VS about this before I run my test. At runtime, I get the error below on the cast:

image

Upon further inspection, it seems that element.FindElementsByName("name2") is returning type:

System.Collections.ObjectModel.ReadOnlyCollection<OpenQA.Selenium.Appium.AppiumWebElement>

which is why I am getting the cast exception.

Why is my nested search from a WindowsElement returning an AppiumWebElement?

My workaround to this is to just replace

IList<WindowsElement> list = (IList<WindowsElement>)element.FindElementsByName("name2");

with

var list = element.FindElementsByName("name2"); list[0].Click();

and I am able to click the desired element without issue. However, it doesn't make much sense that a WebElement should ever be returned, especially when my driver is instantiated as a WindowsDriver<WindowsElement>.

I am not sure if the error is because I am converting from a ReadOnlyCollection to an IList, or from WebElement to WindowsElement because

WindowsElement element1 = driver.FindElementByName("name1"); WindowsElement element2 = (WindowsElement)element1.FindElementByName("name2").Click();

executes without error as well, although the cast is needed which means that the "nesting" here is still returning a WebElement. Can someone confirm whether this is a valid concern? The way I see it, an explicit cast to a WindowsElement shouldn't be necessary when nesting a search like I do above.

caulfield2 avatar Aug 21 '17 14:08 caulfield2

Has anyone has found a solution to this issue yet? I recently ran into the same problem on a project I'm working on for a client & can't seem to find any other references to this issue online (aside from the handful of places @caulfield2 has posted/referenced this). Thanks in advance for any followup!

donschaefer avatar Apr 16 '18 21:04 donschaefer

@donschaefer I have yet to find a way to get a ReadOnlyCollection<WindowsElement> returned from a nested search and have not seen anyone else bring up this issue. Support on this repo seems to be lacking...

caulfield2 avatar Apr 20 '18 19:04 caulfield2

Thanks for the response @caulfield2 - I ended up giving up on this too & ended up building out more complex xpath values as a work-around in the absence of a more elegant solution :(

donschaefer avatar Jun 07 '18 17:06 donschaefer

Hello All, I am still seeing the above issue and I think it is redirected to appium-dot-net-driver issue, but not yet fixed. It is critical for us to get a solution for this. Kindly help.

When we try to get nested FindElement or FindElements, it returns an appiumWebELement.

shaktim121 avatar Jun 20 '19 05:06 shaktim121

Hi guys, any news regarding this issue?

mibs959 avatar Nov 13 '19 09:11 mibs959

I encounter same issue, workaround I used is to cast appiumWebElement as WindowsElement

List<WindowsElement> listMatchingChildControls = new List<WindowsElement>(); ReadOnlyCollection<AppiumWebElement> controlCollection = someWinElementItem.FindElementsByName("AccessibleName");

foreach (AppiumWebElement childControl in controlCollection) { WindowsElement winControl = childControl as WindowsElement; listMatchingChildControls.Add(winControl); }

aeparras avatar Apr 14 '20 14:04 aeparras

@caulfield2 does the workaround suggested above worked for you?

Dor-bl avatar Nov 25 '22 08:11 Dor-bl

@Dor-bl For nested elements declare the element as AppiumWebelement the required actions will be performed even if you declare the nested element as AppiumWebElement and other elements are declared as WindowsElement. Both types work together.

AmolLearning avatar Dec 09 '22 11:12 AmolLearning

Closed since no comment.

Dor-bl avatar Dec 17 '22 13:12 Dor-bl