Description
What happened?
When evaluating the Displayed property of the WebElement. Displayed returns False, but should return True as the element is displayed.
To use the below PowerShell code sample, save the script to a directory that also contains the WebDriver.dll and desired browserdriver.exe
---PowerShell code sample did not display correctly, so I attached as .txt file (Also included in the "How can we reproduce the issue" section---
Example.txt
Execution of the Example outputs:
Notice Displayed is False; yet on the page, the element is definitely displayed.
I have investigated and the issue seems to come from the use of ShadowRoot on the page and is-Displayed.js not able to correctly evaluate it.
The input element that we are checking the Displayed property on hosts a shadow-root
When is-displayed.js evaluates the element, it begins evaluating each element up the tree to the root element. If it were the normal DOM, it would reach the #document element and see this is nodeType 9 and return true if all elements are visible up the tree. In this case, when is-displayed.js begins going up the tree it reaches the "dnx-testfield" which is seen as the shadowRoot. In the case of this page, there is no assignedSlot for the shadowRoot, so null is returned and since null is returned, False is returned for displayed.
This can be seen in the below code; however, I only have the minified .js to work with, so it's a little hard to read.
function c(d) {
if (W(d) && "none" == Y(d, "display"))
return !1;
var e;
if ((e = d.parentNode) && e.shadowRoot && void 0 !== d.assignedSlot)
e = d.assignedSlot ? d.assignedSlot.parentNode : null;
else if (d.getDestinationInsertionPoints) {
var f = d.getDestinationInsertionPoints();
0 < f.length && (e = f[f.length - 1])
}
if (Fc && e instanceof ShadowRoot) {
if (e.host.shadowRoot !== e)
return !1;
e = e.host
}
return !e || 9 != e.nodeType && 11 != e.nodeType ? e && W(e, "DETAILS") && !e.open && !W(d, "SUMMARY") ? !1 : !!e && c(e) : !0
}
In the page when a parent element with a populated shadowRoot property is reached, it goes in the this block of code:
if ((e = d.parentNode) && e.shadowRoot && void 0 !== d.assignedSlot)
e = d.assignedSlot ? d.assignedSlot.parentNode : null;
In the page, the parent element with shadowRoot is dnx.textfield with label "Email"
When this is reached e is set to null because d.assignedSlot (the assignedSlot property value of the current element) is null
Then since e is null and e (the parent element is not type 9 or 11), then it does not return True as it should since the shadowRoot has been reached.
return !e || 9 != e.nodeType && 11 != e.nodeType ? e && W(e, "DETAILS") && !e.open && !W(d, "SUMMARY") ? !1 : !!e && c(e) : !0
It may not be the right solution, but I was able to address this by adding a conditional to the return that if the parent element matched the parent elements shadowroot property, then return the parent element instead of null when there is no assignedSlot.
if ((e = d.parentNode) && e.shadowRoot && void 0 !== d.assignedSlot)
e = d.assignedSlot ? d.assignedSlot.parentNode : d.parentNode == d.parentNode.shadowRoot ? d.parentNode : null;
else if (d.getDestinationInsertionPoints) {
var f = d.getDestinationInsertionPoints();
0 < f.length && (e = f[f.length - 1])
}
How can we reproduce the issue?
Function Get-Browser {
Param (
[Parameter(Mandatory=$true)]
[ValidateSet("Edge","Chrome")]
[string]$type
)
Set-EnvironmentPath
Add-Type -Path "$($PSScriptRoot)\WebDriver.dll"
Switch ($type) {
"edge" {
$Driver = New-Object OpenQA.Selenium.Edge.EdgeDriver
}
"chrome" {
$Driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
}
}
$Driver
}
Relevant log output
Unfortunately I was unable to find a good example of enabling logging with PowerShell, and the logging namespace did not show under OpenQA.Selenium.Internal, so I was not able to include this.
Operating System
Windows 10
Selenium version
PowerShell 5.1.19041.1682
What are the browser(s) and version(s) where you see this issue?
Chrome 121.0.6167.185 and Edge 121.0.2277.128 (both 64-bit)
What are the browser driver(s) and version(s) where you see this issue?
ChromeDriver 120.0.6099.109 (3419140ab665596f21b385ce136419fde0924272-refs/branch-heads/6099@{#1483}) Microsoft Edge WebDriver 120.0.2210.121 (6086ec26a02a23b6723b794ef7b81d37e71506e3)
Are you using Selenium Grid?
No