microsoft / WinAppDriver

Windows Application Driver
MIT License
3.68k stars 1.4k forks source link

Find following element by Xpath using existing element #120

Closed BogdanKliuk closed 5 years ago

BogdanKliuk commented 7 years ago

Need to search element from existing one. Xpah like: driver.findElement(By.xpath("//*[@AutomationId='SomeId']/following::*")) works fine. but if divide this to to separate searches like : driver.findElementByAccessibilityId("SomeId").findElement(By.xpath("./*/following::*")) it throws NoSuchElement Exeption

timotiusmargo commented 7 years ago

Hi @BogdanKliuk,

Can you provide a specific sample that can easily reproduce the issue you saw?

Finding element using XPath starting from certain parent node seems to work fine for me. An example of searching element using XPath within specified parent node can be found on FindElementByXpath test case on https://github.com/Microsoft/WinAppDriver/blob/v0.7-beta/Tests/W3CWebDriver/ElementElement.cs#L159

IOSElement el = alarmTab.FindElementByXPath("//Button[@AutomationId=\"MoreButton\"]") as IOSElement;

The line above generate identical JSON wire protocol request when using OpenQA.Selenium.By var el = alarmTab.FindElement(OpenQA.Selenium.By.XPath("//Button[@AutomationId=\"MoreButton\"]"));

In the test above, an XPath query is performed on alarmTab element that was previously located using AlarmPivotItem accessibility Id. I.e. alarmTab = session.FindElementByAccessibilityId("AlarmPivotItem");

BogdanKliuk commented 7 years ago

Hello. I am not sure if example that you provided is actually searching after this element. xpath without dot like "//Button" starts to search on all DOM, so it will find any first element regardless of base element. ".//Button" starts to search only within children. does this work different here comparing to standard web? Note: version without 'dot' also works for me. but I have multiple elements like that so I am really need to search elements that after some elements (not even children's set). Example that I provided 'driver.findElementByAccessibilityId("SomeId").findElement(By.xpath(".//following::"))' is actually the one I need. what else might help, screen from inspector?

RafalSkorka commented 7 years ago

This does not work because when you first do findElementByAccessibilityId("SomeId") it returns only a subtree of the document with this element as a root. So there is no "following" node. You can only select child nodes like findElementByAccessibilityId("SomeId").findElementsByXPath("//*"); // all children

BogdanKliuk commented 7 years ago

is there any work around?the thing is that parent element don't have IDs actually it has but somehow they do not appear on UI (xamarin render issue) that is why I use so weak path

RafalSkorka commented 7 years ago

Can you find them by tag names or class names? Could you post the xml from driver.PageSource?

hassanuz commented 5 years ago

Please create a new ticket if this issue persists on the latest release of WinAppDriver.

vishnuprakash9845 commented 5 years ago

Hi need some help on "How to get the parent windows element of the current identified windows element". Opposite to "Following" in xpath -__?

forconz commented 2 years ago

This does not work because when you first do findElementByAccessibilityId("SomeId") it returns only a subtree of the document with this element as a root. So there is no "following" node. You can only select child nodes like findElementByAccessibilityId("SomeId").findElementsByXPath("//*"); // all children

_driver.TryFindElementByName("会话").FindElementsByXPath("//*")
0 children

_driver.TryFindElementByName("会话").FindElementsByXPath("//ListItem")
15 children

FindElementsByXPath("//*") is not correct.

liljohnak commented 2 years ago

@forconz correct,

findElementByAccessibilityId("SomeId").findElementsByXPath("*/*"); // all children

findElementByAccessibilityId("SomeId").findElementsByXPath("*//*"); // all descendants

forconz commented 2 years ago

I write Extensions for children and descendants.

all children

        public static ReadOnlyCollection<AppiumWebElement> GetChildren(this WindowsElement element)
        {
            try
            {
                return element.FindElementsByXPath("*/*");
            }
            catch (Exception)
            {
                return null;
            }
        }

all descendants

        public static ReadOnlyCollection<AppiumWebElement> GetDescendants(this WindowsElement element)
        {
            try
            {
                return element.FindElementsByXPath("*//*");
            }
            catch (Exception)
            {
                return null;
            }
        }

But how to find parent element or adjoining element ? Unable to get the parent of the element using xpath, such as /..