硒webdriver(chromedriver)和访问影子dom [英] selenium webdriver (chromedriver) and accessing shadow dom
本文介绍了硒webdriver(chromedriver)和访问影子dom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在测试一个使用影子dom的新应用程序,如下所示:
I'm testing out a new application that uses shadow dom like so:
#shadow-root (open)
<div class="th_filePicker">
<div class="th_fp_header">
<div class="th_fp_title" role="heading" aria-level="1" data-l10n-id="th_fp_title">Select Image</div>
<div class="th_fp_Close"><button class="close-popup" data-l10n-id="close_popup" title="Close"></button></div>
</div>
</div>
有人对我如何访问文件选择器控件中的元素(特别是关闭图标)有任何想法吗?
Does anyone have any idea on how I can access the elements in the file picker control - specifically, the close icon?
推荐答案
您可以尝试这种繁重"的方法(C#,但取决于您的语言,可能是类似的东西):
You can try this "heavy" approach (C# but depending on your language it can be something like that):
public IWebElement DeepFind(By search)
{
try
{
// search a result in the main dom
return Driver.FindElement(search);
}
catch (NoSuchElementException)
{
// if nothing we will take a look to the shadow dom(s)
var shadowRoots = new List<IWebElement>();
try
{
// will use the recursive method that search for all shadow roots
ListShadowRoots(search, Driver.FindElements(By.XPath("//*")), shadowRoots);
}
catch (NoSuchElementException)
{
//
}
// return the first element that match the By search
return shadowRoots.FirstOrDefault(s => s.FindElement(search) != null);
}
}
private void ListShadowRoots(By search, ReadOnlyCollection<IWebElement> elements, List<IWebElement> shadowRoots)
{
elements.ToList().ForEach(e =>
{
var jsResult = (IWebElement)ExecuteJavascript("return arguments[0].shadowRoot", new object[] { e });
if (jsResult != null)
{
shadowRoots.Add(jsResult);
try
{
ListShadowRoots(search, jsResult.FindElements(By.XPath("//*")), shadowRoots);
}
catch (NoSuchElementException)
{
//
}
}
});
}
private object ExecuteJavascript(string code, object[] args)
{
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript(code, args);
}
驱动程序是Web驱动程序(IWebDriver)
Driver is the web driver (IWebDriver)
性能还不错,它可以完成工作;) 希望它可以帮助
Performances are not so bad and it does the job ;) Hope that it can help
这篇关于硒webdriver(chromedriver)和访问影子dom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文