0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iframe>シャドウDOMの先にある要素の取得、操作 --メモ--

0
Posted at

MDNのHTMLのリファレンスで
https://developer.mozilla.org/ja/docs/Web/HTML/Element/select
のselectタグにアクセスしたかったが、Seleniumでエラーが出て取得できなかった。
調べたら、取得したいタグは iframeの中にあり、さらにShadow DOMがあってその中にあった。
通常のシンプルなロケータではうまくいかなかったので、一工夫してみたら上手くいった。

           ChromeDriver driver = new ChromeDriver();

            try
            {
                driver.Manage().Window.Maximize();
                driver.Navigate().GoToUrl("https://developer.mozilla.org/ja/docs/Web/HTML/Element/select");
                var iframe = driver.FindElement(By.CssSelector(".interactive"));
                driver.SwitchTo().Frame(iframe);
                IWebElement shadow_output = driver.FindElement(By.TagName("shadow-output"));
                IWebElement shadow_root = (IWebElement)driver.ExecuteScript("return arguments[0].shadowRoot", shadow_output);
                IWebElement select_pet = shadow_root.FindElement(By.Id("pet-select"));
                var selectObject = new SelectElement(select_pet);
                selectObject.SelectByIndex(2);
            }
            finally
            {
                Console.ReadKey();
                driver.Quit();
            }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?