WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Chaining FindElement does not work properly when more than one can be found

Open matt-stratt opened this issue 6 years ago • 7 comments

I have always been confused as to why the approach like below does not work as I expect

driver.FindElementByName("RadioButton").FindElementByName("Profile")

This approach does not seem to work if that first element you identify, so in this case RadioButton, does not match what you are identifying in the second call.

A more clearer example..

Lets say you have a list of 3 Radio Buttons

  • Settings
  • Profile
  • Administration

If i do this, it works fine... driver.FindElementByName("RadioButton").FindElementByName("Settings")

But if I do this, it does not work driver.FindElementByName("RadioButton").FindElementByName("Profile")

I have tested this and confirmed it to only work if its the first RadioButton to be found. The whole point of this is to provide flexibility without having to use FindElements and then loop through them correct? Is this working as expected?

matt-stratt avatar Jul 07 '20 17:07 matt-stratt

What if you directly use FindElementByName("Profile"), Because for me it always works, and I use the nested approach only if I have multiple element with the same name or same automation ID.

anunay1 avatar Jul 08 '20 15:07 anunay1

Thanks for the response @anunay1 , unfortunately there is an additional Profile button that its also picking up thats why im trying to drill down to just the RadioButton one.

So for that reason exactly is why I am trying to use the nested approach.

matt-stratt avatar Jul 08 '20 15:07 matt-stratt

@Tree55Topz how many items return next code: driver.FindElementByName("RadioButton")? May be you search related to incorrect parent? Could you share Inspector tree?

Shakevg avatar Jul 08 '20 21:07 Shakevg

In my app there are 6 Radio Buttons to select from.

If I do..

var elements = _session.FindElementsByName("RadioButton")

It returns 6 which is as expected. I even added the following to debug

foreach (var element in elements){
     WebElement nestedElement = element.FindElementByClassName("TextBox")
     Console.WriteLine(nestedElement.GetAttribute("Name").ToString());
}

This indeed printed out the 6 radio buttons I am trying to interact with. This is either by design or a bug with WinAppDriver, its like it stops at the first RadioButton.

matt-stratt avatar Jul 08 '20 23:07 matt-stratt

@anunay1 Could it be something with this? Whenever I use more than one element (chaining like above for instance) I have to cast it to WindowsElement. So the actual element instantiation looks like this

WindowsElement btn = (WindowsElement)driver.FindElementByName("RadioButton").FindElementByName("Settings");

I do this because otherwise I get the following exception:

"Cannot implicitly convert type 'OpenQA.Selenium.Appium.AppiumWebElement' to 'OpenQA.Selenium.Appium.Windows.WindowsElement'. An explicit conversion exists (are you missing a cast?)"

If I remember correctly, had to start doing this sometime last year - seems there was some updates to WinAppDriver and things started relying more on Appium. Was forced to actually refactor many things including how the app was launched and passing in app argurments

matt-stratt avatar Jul 09 '20 13:07 matt-stratt

@Tree55Topz driver.FindElementByName("RadioButton") will always return first RadioButton element (this is by Selenium design). If you want to search some child item related to any RadioButton parent item you should use xpath like _session.FindElementsByXpath("//RadioButton//*[@Name='SettingName']")

Shakevg avatar Jul 09 '20 20:07 Shakevg

@Shakevg This worked like a charm - I really need to brush up on my XPath

However, I am still confused as to what the benefit of chaining elements like this is if it just stops at the first one.

Will close this - thanks!

matt-stratt avatar Jul 10 '20 19:07 matt-stratt