1

I'm writing a test which needs to store password inside the browser for a dumb site and I'm performing this task with Selenium Python. I'm able to load chrome://settings/passwords without problem.

The task requires to click the "Add" button and fill the form with dumb data if they are not present. The problem which I am facing is the selection of the button or any other element of the settings page.

This is the HTML element of the button:

<cr-button id="addPasswordButton" title="Add new password" role="button" tabindex="0" aria-disabled="false">
Add

The idea I had was to select it with one of these options:

btn = web_driver.find_element(By.ID, "addPasswordButton")
btn = web_driver.find_element(By.XPATH, "//*[@id='addPasswordButton')]")

but both returns:

RtlGetAppContainerNamedObjectPath [0x773C7B3E+238]

The same error with any other element in the page, exept the settings-ui at top level. I've also tryed to select elements based on the tag name, which seems quite custom, but nothing.

Also searching the error has not given any clue for scarcity of results. It seems to me that the settings page is build with nested subpages and that Selenium is not able to inspect them.

I've also tryed to switch to the inner page without results:

a = web_driver.find_element(By.XPATH, "//settings-ui")
web_driver.switch_to(a)

leads to:

{TypeError}TypeError("'SwitchTo' object is not callable")

Is there a way to solve this issue and being able to select an element which is also provided with an unique ID?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Marco
  • 705
  • 8
  • 28
  • 1
    These are inside shadow roots. They're not accessible directly via the usual element selection functions. For example, `#addPasswordButton` is inside the shadow root of `` which is inside the shadow root of ``. – Ouroborus Jul 04 '23 at 09:07

2 Answers2

1

maybe this answer is helpful, you should go into shadowroot, should try it: Is there any way to get element info inside the shadow root with Selenium?

using above method, it can go into settings-ui

this setting page has so many shadowRoot, be patient.

container = driver.find_element(By.TAG_NAME, "settings-ui")
shadowRoot = driver.execute_script("return arguments[0].shadowRoot", container)
container = shadowRoot.find_element(By.ID, "container").find_element(By.TAG_NAME, "settings-main")
shadowRoot = driver.execute_script("return arguments[0].shadowRoot", container)
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
Jiu_Zou
  • 463
  • 1
  • 4
1

The Add button is under multiple #shadow-root

password-shadow-root


Solution

To click on the element you nned to use document.querySelector() and you can use the following solution:

driver.get("chrome://password-manager/passwords")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((driver.execute_script("return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('settings-autofill-page').shadowRoot.querySelector('passwords-section').shadowRoot.querySelector('cr-button#addPasswordButton')")))).click()

Browser snapshot:

password-shadow-root-click

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352