I am working with the chrome://extensions
page and I met some problems when trying to access a specific element using selenium. I am trying to get the extension id of an extension that I installed. The work flow is as follows:
1. I go to the chrome://extensions
page by using driver.get()
2. I get the "developer mode" toggle by using selenium element selector, and click the toggle so that extension id gets shown on the page.
3. I want to use multiple elements selector to select all "extensions-item" element on the page, find the specific extension that I installed, and retrieve the id. But I am stuck at this part because I am able to select one element but not multiple elements for some reason.
I have the following script:
def expand_shadow_element(driver, element):
shadow_root = driver.execute_script(
'return arguments[0].shadowRoot', element)
return shadow_root
def get_extension_id(driver):
root1 = driver.find_element_by_tag_name("extensions-manager")
shadow1 = expand_shadow_element(driver, root1)
shadow2 = shadow1.find_element_by_css_selector("cr-view-manager")
root3 = shadow2.find_element_by_id("items-list")
shadow3 = expand_shadow_element(driver, root3)
i = shadow3.find_element_by_tag_name("extensions-item") # This works fine, but it can only retrieve the first "extensions-item". What if I want the second one?
print("finding single element successful")
items = shadow3.find_elements_by_tag_name("extensions-item") # This line throws an error
for item in items:
shadow4 = expand_shadow_element(driver, item)
if shadow4.find_element_by_id("name").text != "Chrome Automation Extension":
return "Success"
raise "Error"
The error message is as follows:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"extensions-item"}
(Session info: chrome=78.0.3904.97)
Besides, I used find_element_by_tag_name
instead of others because others do not work. To my understanding, even if I believe it is essentially the same as find_element_by_css_selector
, but using this instead will throw element not found error. I do not understand why.
Anyone has any idea? I just don't know why I am able to locate one element but not a list of them. I am guessing it is because the way I manipulated shadow-dom, but through searching on the Internet I was not able to find another way of manipulating it. Any help is appreciated, thank you!