# Getting elements within shadowroots using Powershell and Selenium C# Webdriver

**URL:** <https://forums.powershell.org/t/getting-elements-within-shadowroots-using-powershell-and-selenium-c-webdriver/15900>\
**Category:** DSC\
**Created:** [March 8, 2021, 5:48pm UTC](https://forums.powershell.org/t/getting-elements-within-shadowroots-using-powershell-and-selenium-c-webdriver/15900 "2021-03-08T17:48:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgohing](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rgohing/32/1091_2.png) [@rgohing](https://forums.powershell.org/u/rgohing)\
**Post date:** [March 8, 2021, 5:48pm UTC](https://forums.powershell.org/t/getting-elements-within-shadowroots-using-powershell-and-selenium-c-webdriver/15900/1 "2021-03-08T17:48:07Z")

</div>

I am trying to automate clicks in a chrome browser using Powershell and Selenium. So far, I have managed to click elements within the body of the html file - sample below.

**HTML**

```auto
<label for="cars">Choose a car:</label>

<select name="cars" id="cars" class="dropdownContent">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>

```

**Powershell**

```auto
$ChromeDriver.FindElementByXPath('//*[@id="cars"]/option[3]').Click()

```

Using the script above will click the 3rd option that resides within the select element (in this case, “Mercedes”).

However, I am struggling in manipulating elements inside Shadow roots, i can’t seems to access any elements inside it, what makes it worse is that the element that I would like to click on resides within child shadow roots, as seen in the image below:

**HTML**

```auto
<mds-select class="col-md-3 link-section mds-select--cpo" id="linkCategoryId">
<!--1st Shadow Root, open-->
    <div class="mds-select__label-wrap">
        <label class="mds-select __label mds-select__ label--box" id="select-linkCategoryId-label">Object</label>
    </div>
    <div class="mds-select __select-wrap mds-select__ select-wrap--vertical-label">
        <div class="mds-select __menu mds-select__ menu--hidden">
            <div class="mds-select__menu-list">
                <mds-select-option data-index="0" data-setsize="6" value="Monkey" label="Monkey" selected="false" data-accessible-text-cardinality-separator="of" inactive="false" class="mds-select-option--cpo">
                <!--2nd Shadow Root within, open-->
                
                    <div class="option option--hidden" tabindex="-1" role="option" aria-disabled="false" aria-posinset="1" aria-setsize="6">
                        <div class="option__label">
                            <span class="option__accessible-text">Monkey</span>
                            <span class="option__label-primary" aria-hidden="true">Monkey</span>
                            <div class="option__slot-container" aria-hidden="true">
                                <slot></slot>
                            </div>
                        </div>
                    </div>
                
                </mds-select-option>
                
                <mds-select-option data-index="1" data-setsize="6" value="Mochi" label="Mochi" selected="false" data-accessible-text-cardinality-separator="of" inactive="false" class="mds-select-option--cpo">
                <!--2nd Shadow Root within, open-->
                
                    <div class="option option--hidden" tabindex="-1" role="option" aria-disabled="false" aria-posinset="1" aria-setsize="6">
                        <div class="option__label">
                            <span class="option__accessible-text">Mochi</span>
                            <span class="option__label-primary" aria-hidden="true">Mochi</span>
                            <div class="option__slot-container" aria-hidden="true">
                                <slot></slot>
                            </div>
                        </div>
                    </div>
                
                </mds-select-option>
            </div>
        </div>
    </div>
</mds-select>

```

Specifically, i would like to click on “Monkey”

I tried using xpath “$ChromeDriver. **FindElementByXPath** (‘//\*[@id=“linkCategoryId”]//div/div[2](https://i.stack.imgur.com/p8QWp.png)/div/div/div/mds-select-option[1](https://i.stack.imgur.com/vdXsn.png)’).Click()”

but I’m just getting an error log that says “Unable to locate element”:

I would greatly appreciate if someone can help me, provide tips, or point me in the right direction.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [March 9, 2021, 4:25pm UTC](https://forums.powershell.org/t/getting-elements-within-shadowroots-using-powershell-and-selenium-c-webdriver/15900/2 "2021-03-09T16:25:08Z")

</div>

Personally use dot notation more than XPath as it allow me to just quickly walk the path to get to the data. Once that is done, it looks like there are two extra divs in the path and the 1 should have square brackets:

```auto
[xml]$test = @"
<html>
    <head>
    </head>
    <body>
        <mds-select class="col-md-3 link-section mds-select--cpo" id="linkCategoryId">
        <!--1st Shadow Root, open-->
            <div class="mds-select__label-wrap">
                <label class="mds-select __label mds-select__ label--box" id="select-linkCategoryId-label">Object</label>
            </div>
            <div class="mds-select __select-wrap mds-select__ select-wrap--vertical-label">
                <div class="mds-select __menu mds-select__ menu--hidden">
                    <div class="mds-select__menu-list">
                        <mds-select-option data-index="0" data-setsize="6" value="Monkey" label="Monkey" selected="false" data-accessible-text-cardinality-separator="of" inactive="false" class="mds-select-option--cpo">
                        <!--2nd Shadow Root within, open-->
                        
                            <div class="option option--hidden" tabindex="-1" role="option" aria-disabled="false" aria-posinset="1" aria-setsize="6">
                                <div class="option__label">
                                    <span class="option__accessible-text">Monkey</span>
                                    <span class="option__label-primary" aria-hidden="true">Monkey</span>
                                    <div class="option__slot-container" aria-hidden="true">
                                        <slot></slot>
                                    </div>
                                </div>
                            </div>
                        
                        </mds-select-option>
                        
                        <mds-select-option data-index="1" data-setsize="6" value="Mochi" label="Mochi" selected="false" data-accessible-text-cardinality-separator="of" inactive="false" class="mds-select-option--cpo">
                        <!--2nd Shadow Root within, open-->
                        
                            <div class="option option--hidden" tabindex="-1" role="option" aria-disabled="false" aria-posinset="1" aria-setsize="6">
                                <div class="option__label">
                                    <span class="option__accessible-text">Mochi</span>
                                    <span class="option__label-primary" aria-hidden="true">Mochi</span>
                                    <div class="option__slot-container" aria-hidden="true">
                                        <slot></slot>
                                    </div>
                                </div>
                            </div>
                        
                        </mds-select-option>
                    </div>
                </div>
            </div>
        </mds-select>
    </body>
</html>
"@

#Dot Notation
$test.html.body.'mds-select'.div.div.div.'mds-select-option'[0]
#XPath
$path = '//mds-select/div/div/div/mds-select-option[1]'
Select-Xml -Xml $test.html.body -XPath $path | Select -ExpandProperty Node

```

**Output:**

```auto
PS C:\Users\rasim\Dropbox\GitHub\Powershell\RemedyForce> $test.html.body.'mds-select'.div.div.div.'mds-select-option'[0]

data-index : 0
data-setsize : 6
value : Monkey
label : Monkey
selected : false
data-accessible-text-cardinality-separator : of
inactive : false
class : mds-select-option--cpo
#comment : 2nd Shadow Root within, open
div : div

PS C:\Users\rasim\Dropbox\GitHub\Powershell\RemedyForce> $path = '//mds-select/div/div/div/mds-select-option[1]'
Select-Xml -Xml $test.html.body -XPath $path | Select -ExpandProperty Node

data-index : 0
data-setsize : 6
value : Monkey
label : Monkey
selected : false
data-accessible-text-cardinality-separator : of
inactive : false
class : mds-select-option--cpo
#comment : 2nd Shadow Root within, open
div : div

```

---

<div class="post-metadata">

**Author:** ![rgohing](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rgohing/32/1091_2.png) [@rgohing](https://forums.powershell.org/u/rgohing)\
**Post date:** [March 9, 2021, 5:22pm UTC](https://forums.powershell.org/t/getting-elements-within-shadowroots-using-powershell-and-selenium-c-webdriver/15900/3 "2021-03-09T17:22:07Z")

</div>

Hi @rob-simmers, thanks for responding and the tips. Would you know how I could use it to access the elements within child of Shadow roots using Selenium C# Webdriver?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [March 9, 2021, 6:29pm UTC](https://forums.powershell.org/t/getting-elements-within-shadowroots-using-powershell-and-selenium-c-webdriver/15900/4 "2021-03-09T18:29:20Z")

</div>

Have not used Selenium, but based on what you posted and the XPath I provided, my assumption would be something like:

```auto
$ChromeDriver.FindElementByXPath('//*[@id="mds-select"]/div/div/div/mds-select-option[1]').Click()

```

---

<div class="post-metadata">

**Author:** ![konalion](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/konalion/32/1962_2.png) [@konalion](https://forums.powershell.org/u/konalion)\
**Post date:** [December 18, 2021, 3:22am UTC](https://forums.powershell.org/t/getting-elements-within-shadowroots-using-powershell-and-selenium-c-webdriver/15900/5 "2021-12-18T03:22:15Z")

</div>

@rob-simmers so how would you do this without Selenium?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:14pm UTC](https://forums.powershell.org/t/getting-elements-within-shadowroots-using-powershell-and-selenium-c-webdriver/15900/6 "2024-05-16T21:14:13Z")

</div>

Closed on May 16, 2024, 9:14pm UTC


