pythonselenium-webdriverautomationui-automationnon-interactive

How to click element which is non-interactable using selenium-python


I've a portal which I need to automate. The problem arises when I'm trying to click on the Accept All button element of dialog box. But, I'm not able to locate it when trying to use inspect (F12).

Following is the HTML code:

<div class="sc-jsJBEP iXSECa">
    <div data-testid="uc-buttons-container" style="margin: 0px -6px;" class="sc-eeDRCY fnyekb">
        <button aria-label="More Information" role="button" data-testid="uc-more-button" style="margin: 0px 6px;" class="sc-dcJsrY kvOhCL">Settings</button>
        <button role="button" data-testid="uc-deny-all-button" style="margin: 0px 6px;" class="sc-dcJsrY dExLQu">Deny</button>
        <button role="button" data-testid="uc-accept-all-button" style="margin: 0px 6px;" class="sc-dcJsrY iYvtyJ">Accept All</button>
    </div>
</div>

Here is the button code I need to click:

<button role="button" data-testid="uc-accept-all-button" style="margin: 0px 6px;" class="sc-dcJsrY iYvtyJ">Accept All</button>

But, when I'm trying to even inspect the element, I'm not able to locate it. Tried multiple xpaths but not able to locate in any:

Xpath used = "//button[@data-testid='uc-accept-all-button']" xpath_1

XPath used = "//button[text()='Accept All']" XPath_2

This is my selenium code in python: Selenium Code in Python

accept_button = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, XPATH_AcceptAllBtn))
    )

accept_button.click()

The error message I'm getting in console while clicking on that element: Error message in console

Let me know if anything else info is needed.


Solution

  • You're getting blocked by a shadow DOM. You need to find the element acting as the shadow root (you can see this in Chrome's inspector...look for #shadow-root)

    Based on your screenshots, I found the website you were working on and this worked for me. Your more direct XPATH method will probably work as well once you pierce the shadow DOM veil.

    y = driver.find_element(By.ID,'usercentrics-root').shadow_root
    
    focus_wall = y.find_element(By.ID,'focus-lock-id')
    for x in focus_wall.find_elements(By.TAG_NAME,'button'):
        if x.get_attribute("innerText") == 'Accept All': x.click()