question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[šŸš€ Feature]: Python Difficulties with ShadowRoot object interface

See original GitHub issue

Feature and motivation

The ShadowRoot object interface differs from the WebElement object interface in terms of keyword arguments on some similar functions:

Currently the ShadowRoot object find_element and find_elements functions have the signature: def find_element(self, using, value):

def find_elements(self, using, value):

however the webelement objects have:

def find_element(self, by=By.ID, value=None):

def find_elements(self, by=By.ID, value=None):

The issue with this is that it prevents using the shadow root elements and the web elements when duck typing as if you try and use something like:

element.find_element(by=By.NAME, value='test-element')

this wonā€™t work when the element happens to be a shadow root as the keyword arguments donā€™t line up (forcing you to not use keyword arguments).

Another issue iā€™ve run into when migrating to selenium 4 is that the ShadowRoot class is hidden from the interface, which i think is because in the Java and .net versions of the package the aim is to use the SearchContext, however this isnā€™t available in the python package and so it feels a bit difficult to use as a result e.g.

  • theres no option to overwrite the internal functions with adjusted ones.
  • The exception NoSuchShadowRootException doesnā€™t seem to be public on the exceptions module
  • When you get a detached shadow root it seems to get wrapped into a NoSuchElementException whereas it would be nice if we could react to the shadow root becoming detached (similarly to a stale element exception) rather than it looking like the element doesnā€™t exist.

Usage example

Aligning the interfaces between shadow roots and webelements would mean we could treat them similarly when a function might return either a shadow root or a web element and then whatever it returns we want to then search inside that element for another element, currently you need to treat them differently or not use keyword arguments.

If the ShadowRoot object was made public we would be able to monkeypatch functions within it, this can be useful for example we have a complicated dom structure where we want to search through elements and their shadow roots without needing to be too specific, e.g. find_element(By.NAME, ā€˜publish-buttonā€™) - This publish-button is unique on the page but we donā€™t want to specify exactly which shadow root / element it is inside, in the past we have overwritten the find_element function to ignore certain shadow roots as they contain a lot of nested elements, which would take a long time.

Being able to import the NoSuchShadowRootException without getting warnings from your IDE would be a QOL change.

Being able to distinguish between a shadowroot becoming detached and the element not existing at all would be useful in error handling / retries.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
titusfortnercommented, Dec 13, 2021

We need to get the rst files figured out as part of this as well.

1reaction
titusfortnercommented, Dec 13, 2021

Updating the method to use keywords seems straightforward. #10138

I can verify that PyCharm complains about importing the exception, even though it is able to find it when run. Iā€™m going to guess that it decides what to complain about based on what is in the *.rst files. @AutomatedTester the NoSuchShadowRootException is not showing up in the autosummary section in selenium.common.exceptions.rst. Same thing with shadowroot.py not having an *.rst file. How does one generate these files?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle elements inside Shadow DOM from Selenium
I recently try to access the content settings(see code below) and it has more than one shadow root elements imbricated now you cannot...
Read more >
Steps To Understanding the Shadow DOM - Medium
This is the object that represents the root node of your newly created Shadow DOM and it's where you will append your other...
Read more >
Shadow DOM in Selenium - Titus on Testing
In Selenium 4.1, the code would work, except a ShadowRoot only uses the newest find_element() methods, so you need to update to use...
Read more >
Read Web Components | Leanpub
The ShadowRoot interface of the Shadow DOM API is the root node of a DOM subtree that is rendered separately from a document's...
Read more >
Dmitry Avtonomov
Institute for Energy Problems of Chemical Physics. MSc, Applied Physics and Mathematics, 2008. Moscow Institute of Physics and Technology (MIPT)Ā ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found