camoufox icon indicating copy to clipboard operation
camoufox copied to clipboard

Attempting to use shadowRootUnl

Open sadikhan918 opened this issue 1 year ago • 3 comments

Hey, this might be a silly question but I'm trying to use element.shadowRootUnl, which was implemented earlier today, but I'm not entirely sure how to do it. This is what I've attempted so far:

config = {
    'showcursor': True,
    'forceScopeAccess': True
}

with Camoufox(headless=False, config=config) as browser:
    browserContext = browser.new_context()
    page = browserContext.new_page()
    
    page.goto('random-web-page')
    element = page.locator(".element-class")
    
    shadowRoots = element.shadowRootUnl

But this throws an error because the locator class does not have a shadowRootUnl attribute. How would this be done to get the shadow roots?

sadikhan918 avatar Feb 11 '25 19:02 sadikhan918

Hello,

.shadowRootUnl is meant to be used in page.evaluate:

with Camoufox(headless=False, config={'forceScopeAccess': True}) as browser:
    page = browser.new_page()
    
    # Testing site with a closed shadow root
    page.goto('https://camoufox.com/tests/shadowdom')

    # Pull data (for example, an element's coordinates), or interact with elements in the closed shadow root:
    page.evaluate('document.querySelector("#shadow-host").shadowRootUnl.querySelector("input").click()')

Sorry for the lack of documentation, I haven't gotten around to updating it yet for the latest changes made in beta 21.

I'll look into how I can make locator natively search within closed shadow roots.

daijro avatar Feb 12 '25 16:02 daijro

Hello,

.shadowRootUnl is meant to be used in page.evaluate:

with Camoufox(headless=False, config={'forceScopeAccess': True}) as browser: page = browser.new_page()

# Testing site with a closed shadow root
page.goto('https://camoufox.com/tests/shadowdom')

# Pull data (for example, an element's coordinates), or interact with elements in the closed shadow root:
page.evaluate('document.querySelector("#shadow-host").shadowRootUnl.querySelector("input").click()')

Sorry for the lack of documentation, I haven't gotten around to updating it yet for the latest changes made in beta 21.

I'll look into how I can make locator natively search within closed shadow roots.

I see, thank you. Would this work for iframes with strict CORS access? It seems that I can get the iframe in a shadowroot but I cannot get the contents within it.

sadikhan918 avatar Feb 12 '25 19:02 sadikhan918

Here is an example how you can implement it for closed shadow roots with CORS: https://github.com/techinz/camoufox-captcha

techinz avatar Jun 06 '25 21:06 techinz