WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Driver found the element but could not click because of the "An element command failed because the referenced element is no longer attached to the DOM." exception

Open ghost opened this issue 3 years ago • 23 comments

When I tried to click on a hyperlink element, it throws the "An element command failed because the referenced element is no longer attached to the DOM." exception, even though the element is displayed on the window that driver is focusing on.

Here is my code: var element = driver.FindElementByName("event#LinkId=1"); element.Click();

The exception was thrown at line 2. The same exception was thrown when I tried to use MoveToElement() or element.Location.X.

But it works well if clicking on the parent element: driver.FindElementByAccessibilityId("linkText").Click();

Another issue, the driver could not find the element if using: driver.FindElementByXPath("//Document[@AutomationId=\"linkText\"]/Hyperlink[contains(@Name, 'event')]");

Below is some properties of the hyperlink element: ControlType = Hyperlink(50005) IsEnabled = True IsOffscreen = False IsKeyboardFocusable = False HasKeyboardFocus = False

Does anyone knows how to fix this issue? Thanks!

ghost avatar Dec 10 '22 01:12 ghost

Another issue, the driver could not find the element if using: driver.FindElementByXPath("//Document[@AutomationId="linkText"]/Hyperlink[contains(@Name, 'event')]"); => Save PageSource and check that this XPath is working via http://xpather.com/

Here is my code: var element = driver.FindElementByName("event#LinkId=1"); element.Click(); => What if you add some Sleep before it?

Shakevg avatar Dec 11 '22 12:12 Shakevg

Thanks @Shakevg for your answer.

I tried on http://xpather.com/

No elements found if XPath is: //Document[@AutomationId="linkText"]/Hyperlink[contains(@name, 'event')] Or //Document[@AutomationId="linkText"]/Hyperlink[contains(@Name, 'event')] Or //Document[@AutomationId="linkText"]//Hyperlink[contains(@Name, 'event')] Or //Document[@AutomationId="linkText"]//Hyperlink[contains(@name, 'event')]

Elements found: 1 if XPath is: //Hyperlink[contains(@Name, 'event')] Or //Document[@AutomationId="linkText"]

But when I applied //Hyperlink[contains(@Name, 'event')] into my code, it throws exception (even though the driver is desktop session)

image

Here is my code: var element = driver.FindElementByName("event#LinkId=1"); element.Click(); => What if you add some Sleep before it? => I added some Sleep before it but still get exception: image

Do you think the property IsKeyboardFocusable = False has something to do with those exceptions?

ghost avatar Dec 12 '22 16:12 ghost

Does anyone has any idea about this issue?

I used try {} catch {} to debug the code as below:

image

Every DriverContext.Driver.FindElementByXPath("//*[contains(@Name,'event#')]") step has the element found. No error.

But every eventLink.Click(); step, it throws the "An element command failed because the referenced element is no longer attached to the DOM." exception

image

ghost avatar Dec 12 '22 18:12 ghost

@tienletesting could you provide WinAppDriver logs produced during that code execution?

pm90pl avatar Dec 13 '22 15:12 pm90pl

@pm90pl Here is it: _`========================================== POST /session/<...>/element HTTP/1.1 Accept: application/json, image/png Connection: Keep-Alive Content-Length: 48 Content-Type: application/json;charset=utf-8 Host: User-Agent: selenium/3.141.0 (.net windows) X-Idempotency-Key:

{"using":"accessibility id","value":"linkTexts"} HTTP/1.1 200 OK Content-Length: 97 Content-Type: application/json

{"sessionId":"E107CB4F-8BAA-4E51-9C3F-CA90908A6D84","status":0,"value":{"ELEMENT":"42.27919650"}}

========================================== POST /session/E107CB4F-8BAA-4E51-9C3F-CA90908A6D84/element/42.27919650/click HTTP/1.1 Accept: application/json, image/png Connection: Keep-Alive Content-Length: 2 Content-Type: application/json;charset=utf-8 Host: User-Agent: selenium/3.141.0 (.net windows) X-Idempotency-Key:

{} HTTP/1.1 200 OK Content-Length: 63 Content-Type: application/json

{"sessionId":"E107CB4F-8BAA-4E51-9C3F-CA90908A6D84","status":0}

========================================== POST /session/E107CB4F-8BAA-4E51-9C3F-CA90908A6D84/element HTTP/1.1 Accept: application/json, image/png Connection: Keep-Alive Content-Length: 57 Content-Type: application/json;charset=utf-8 Host: User-Agent: selenium/3.141.0 (.net windows) X-Idempotency-Key:

{"using":"xpath","value":"//*[contains(@Name,'event#')]"} HTTP/1.1 200 OK Content-Length: 101 Content-Type: application/json

{"sessionId":"E107CB4F-8BAA-4E51-9C3F-CA90908A6D84","status":0,"value":{"ELEMENT":"42.27919650.4.0"}}

========================================== POST /session/E107CB4F-8BAA-4E51-9C3F-CA90908A6D84/element/42.27919650.4.0/click HTTP/1.1 Accept: application/json, image/png Connection: Keep-Alive Content-Length: 2 Content-Type: application/json;charset=utf-8 Host: User-Agent: selenium/3.141.0 (.net windows) X-Idempotency-Key:

{} HTTP/1.1 400 Bad Request Content-Length: 160 Content-Type: application/json

{"status":10,"value":{"error":"stale element reference","message":"An element command failed because the referenced element is no longer attached to the DOM."}}

========================================== POST /session/E107CB4F-8BAA-4E51-9C3F-CA90908A6D84/element HTTP/1.1 Accept: application/json, image/png Connection: Keep-Alive Content-Length: 57 Content-Type: application/json;charset=utf-8 Host: User-Agent: selenium/3.141.0 (.net windows) X-Idempotency-Key:

{"using":"xpath","value":"//*[contains(@Name,'event#')]"} HTTP/1.1 200 OK Content-Length: 101 Content-Type: application/json

{"sessionId":"E107CB4F-8BAA-4E51-9C3F-CA90908A6D84","status":0,"value":{"ELEMENT":"42.27919650.4.0"}}

========================================== POST /session/E107CB4F-8BAA-4E51-9C3F-CA90908A6D84/element/42.27919650.4.0/click HTTP/1.1 Accept: application/json, image/png Connection: Keep-Alive Content-Length: 2 Content-Type: application/json;charset=utf-8 Host: User-Agent: selenium/3.141.0 (.net windows) X-Idempotency-Key:

{} HTTP/1.1 400 Bad Request Content-Length: 160 Content-Type: application/json

{"status":10,"value":{"error":"stale element reference","message":"An element command failed because the referenced element is no longer attached to the DOM."}}_ `

ghost avatar Dec 13 '22 16:12 ghost

@tienletesting could you also provide 'PageSource'?

pm90pl avatar Dec 14 '22 11:12 pm90pl

@pm90pl here is it:

image

image

ghost avatar Dec 14 '22 16:12 ghost

@tienletesting ok, can't say anything right now. Is it the only element that can be found using given XPath? Can you perform any other operation on found element? Could you try to take w screenshot of the element using WinAppDriver?

pm90pl avatar Dec 15 '22 09:12 pm90pl

@pm90pl I am sure it is the only element that can be found using the given XPath. I could not perform any other operation on the found element. All operation likes Click, get Location/Size, MoveToElement... throws ""An element command failed because the referenced element is no longer attached to the DOM." exception".

I could not take screenshot for that specific element because the exception throws when trying to get location of the element.

var linkTexts = DriverContext.Driver.FindElementByAccessibilityId("linkTexts"); var eventLink = linkTexts.FindElementByXPath("//*[contains(@Name,'event#LinkId')]"); TakeScreenshot(DriverContext.Driver, eventLink);

image

ghost avatar Dec 15 '22 18:12 ghost

What is shown in inepect.exe?

anunay1 avatar Dec 16 '22 13:12 anunay1

@anunay1 Here is it

image

ghost avatar Dec 16 '22 15:12 ghost

Please use inspect.exe and share what is shown as it has more information.

anunay1 avatar Dec 16 '22 15:12 anunay1

@anunay1 Here image

ghost avatar Dec 16 '22 16:12 ghost

The name is dynamic, with the below code try to get the child elements first: driver.findelementbyname("document").findelementsbyxpth("/")

and check if you get any element.

anunay1 avatar Dec 16 '22 17:12 anunay1

@anunay1 No, I dont get any element. "linkTexts" is AutomationID of the "document" tag.

image

If I use FindElementByXPath("//*[contains(@Name,'event#LinkId')]"), then I get one element:

image

If I use FindElementByXPath("/*[contains(@Name,'event#LinkId')]"), then exception thrown:

image

ghost avatar Dec 16 '22 17:12 ghost

Can you try driver.findelementbyautomatioid("linkTexts").findelementsbytagname("Link")

anunay1 avatar Dec 16 '22 17:12 anunay1

@anunay1 No. No elements found if using driver.findelementbyautomatioid("linkTexts").FindElementsByTagName("Link") or driver.findelementbyautomatioid("linkTexts").FindElementsByTagName("Hyperlink")

image

ghost avatar Dec 16 '22 18:12 ghost

That's really weird. Can you ping me on teams?

anunay1 avatar Dec 16 '22 18:12 anunay1

@anunay1 Yes, that's weird. I can't. I am sorry. Do you need any other information?

ghost avatar Dec 16 '22 18:12 ghost

@tienletesting like this? driver.findelementbyname("document").findelementsbyxpth("*/*")

liljohnak avatar Dec 16 '22 20:12 liljohnak

@liljohnak Yes, it found 2 elements as expected. In "document", besides the "eventLink" link, there is another link.

image

If I tried to click or getLocation of the elements, the "An element command failed because the referenced element is no longer attached to the DOM." exception thrown for both of them:

image

ghost avatar Dec 16 '22 20:12 ghost

I am facing the same issue ,using WinAppDriver to automate Excel. Is there any update on this

perfectprocrastinator avatar Jul 23 '25 07:07 perfectprocrastinator

I am facing the same issue ,using WinAppDriver to automate Excel. Is there any update on this

You need to retry. That's the only way out.

anunay1 avatar Jul 24 '25 14:07 anunay1