appium / dotnet-client

Extension to the official Selenium dotnet webdriver
Apache License 2.0
383 stars 188 forks source link

"Nested" FindElementsBy returns WebElements instead of WindowsElements #188

Closed caulfield2 closed 1 year ago

caulfield2 commented 7 years ago

Description

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

Environment

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

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.

donschaefer commented 6 years ago

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!

caulfield2 commented 6 years ago

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

donschaefer commented 6 years ago

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 :(

shaktim121 commented 5 years ago

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.

mibs959 commented 5 years ago

Hi guys, any news regarding this issue?

aeparras commented 4 years ago

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

List listMatchingChildControls = new List(); ReadOnlyCollection controlCollection = someWinElementItem.FindElementsByName("AccessibleName");

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

Dor-bl commented 1 year ago

@caulfield2 does the workaround suggested above worked for you?

AmolLearning commented 1 year ago

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

Dor-bl commented 1 year ago

Closed since no comment.