Hi,
I am stuck on why I cannot click any element that is located inside the iframe element which lies inside the shadow root (open). Selenium throws WebDriverException saying "unknown error: no element reference returned by script". The whole page has several shadow root elements. And I am able to interact with any element inside them (like Click, SendKeys) using Selenium. But there is this one shadow root that contains the iframe and it drives me crazy why I cannot click anything anymore.
Switching to iframe is correct. First I read the shadowRoot property of the host element(using js). After that, I locate the iframe and switch to it. Then I can locate any element inside that iframe and after all, I have a valid reference to an element I am targeting. Moreover, I can simulate a click with SendKeys(Keys.Enter) invoked on an element, it really works. JS click with ExecuteScript() also works. That is how I am dealing with this situation right now. But I start facing this issue where I do need a real-user click, not js.
If you have any thoughts about what I might be doing wrong, please share them with me. Below is a piece of code I am using to access that iframe and then locate other web elements within it and perform some actions on them. Nothing special, trivial operations.
var shadowHost = driver.FindElement(By.XPath("//some xpath")); // element containing shadowRoot;
var iframe = GetShadowRoot(shadowHost).FindElement(By.CssSelector("iframe"));
var drv = driver.SwitchTo().Frame(iframe);
var buttonElement = drv.FindElement(By.CssSelector("button")); // has a valid reference to an element
buttonElement.Click(); // throws exception, see above in the description public
public IWebElement GetShadowRoot(IWebElement element)
{
var js = driver as IJavaScriptExecutor;
var shadowRoot = (IWebElement)js.ExecuteScript("return arguments[0].shadowRoot", element);
return shadowRoot;
}
var shadowHost = driver.FindElement(By.XPath("//some xpath")); // element containing shadowRoot;
var iframe = GetShadowRoot(shadowHost).FindElement(By.CssSelector("iframe"));
var drv = driver.SwitchTo().Frame(iframe);
var buttonElement =
drv.FindElement(By.CssSelector("button")); // has a valid reference
to an element
var buttonElement = driver.FindElement(By.CssSelector("button"));
buttonElement.Click();
Cheers,
Thalesh
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/669a13c3-3956-4693-9b04-86d8579eb98cn%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/V3Su39OVrEI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAFPrHOCpDo2VsE%2Bqcw71yZWW9%2BJBkFtianOxLeE_4xo5rP_TLg%40mail.gmail.com.
PS: I am the founder of Zylitics, an end to end testing platform for web apps that makes it extremely easy to write, run and debug tests right from a web browser. It needs no configuration and installations. Everything you need is available out of the box, be it different OS`s, browsers, live preview, code management, test assets management and everything in between. It is based on selenium/webdriver but uses a new and extremely easy to use and learn language. https://zylitics.io