Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the shadow root of a web element.
Selenium Java
WebElement host = driver.findElement(By.cssSelector("custom-element")); SearchContext shadowRoot = host.[1]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using getShadowDom() or getShadow() which do not exist.
Trying to access shadow root directly without calling a method.
✗ Incorrect
The method shadowRoot() is used in Selenium Java to access the shadow root of a shadow host element.
2fill in blank
mediumComplete the code to find an element inside the shadow root.
Selenium Java
WebElement host = driver.findElement(By.cssSelector("custom-element")); SearchContext shadowRoot = host.shadowRoot(); WebElement innerElement = shadowRoot.[1](By.cssSelector("button.submit"));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using findElements which returns a list instead of a single element.
Using querySelector which is not a Selenium method.
✗ Incorrect
Inside the shadow root, findElement(By) is used to locate a single element.
3fill in blank
hardFix the error in accessing a shadow DOM element by completing the code.
Selenium Java
WebElement host = driver.findElement(By.cssSelector("custom-element")); WebElement innerElement = host.[1]().findElement(By.cssSelector("button.submit"));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling findElement directly on the shadow host for shadow DOM elements.
Using getShadowRootElement which is not a valid method.
✗ Incorrect
You must first get the shadow root using shadowRoot() before finding elements inside it.
4fill in blank
hardFill both blanks to correctly access a nested shadow DOM element.
Selenium Java
WebElement host = driver.findElement(By.cssSelector("outer-element")); SearchContext outerShadow = host.[1](); WebElement innerHost = outerShadow.findElement(By.cssSelector("inner-element")); SearchContext innerShadow = innerHost.[2]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using findElement instead of shadowRoot for the second blank.
Using getShadowRootElement which is not a valid method.
✗ Incorrect
To access nested shadow DOMs, you call shadowRoot() on each shadow host element.
5fill in blank
hardFill all three blanks to find a button inside a nested shadow DOM and click it.
Selenium Java
WebElement host = driver.findElement(By.cssSelector("outer-element")); SearchContext outerShadow = host.[1](); WebElement innerHost = outerShadow.findElement(By.cssSelector("inner-element")); SearchContext innerShadow = innerHost.[2](); WebElement button = innerShadow.[3](By.cssSelector("button.submit")); button.click();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using clickElement instead of findElement to get the button.
Using getShadow instead of shadowRoot for shadow roots.
✗ Incorrect
You get shadow roots for outer and inner hosts, then find the button element before clicking it.