Selenium C#中无法访问iframe内嵌PDF元素的问题求助
Selenium C#中无法访问iframe内嵌PDF元素的问题求助
嗨,各位大佬!我最近在做Selenium自动化的时候,遇到了iframe内嵌PDF查看器的元素定位问题,卡了好一阵子,想请教下大家怎么解决。
首先跟大家说下页面的结构:
初始加载时,页面里是用来承载PDF的iframe,结构如下:
<div> <iframe src="link.pdf" width="100%" height="500"> #document <!DOCTYPE html> <html> <head></head> <body style="height: 100%; width: 100%; overflow: hidden; margin:0px; background-color: rgb(82, 86, 89);"><embed name="123333" style="position:absolute; left: 0; top: 0;" width="100%" height="100%" src="about:blank" type="application/pdf" internalid="1234"></body></html> </iframe> </div>
当PDF加载完成后,通过开发者工具可以看到实际的DOM变成了带shadow DOM的PDF查看器结构:
<body> <pdf-viewer id="viewer"></pdf-viewer> > #shadow-root(open) <viewer-toolbar id="toolbar"> > #shadow-root(open) <div id="toolbar"> <div id="start"> <cr-icon-button id="sidenavToggle" title="Menu" aria-label="Menu" iron-icon="cr20:menu" aria-expanded="true" role="button" tabindex="0" aria-disabled="false"> </cr-icon-button> <span id="title"><!--?lit$02748104126$-->D5F1C5BE75754D2065258671002817C2_GAQC_Alert_No._421.pdf</span> </div> <div id="center"> <viewer-page-selector style="--page-length-digits: 1;"> </viewer-page-selector> <span class="vertical-separator"></span> <span id="zoom-controls"> <cr-icon-button title="Zoom out" aria-label="Zoom out" iron-icon="pdf:remove" role="button" tabindex="0" aria-disabled="false"> </cr-icon-button> <input type="text" value="100%" aria-label="Zoom level"> <cr-icon-button title="Zoom in" aria-label="Zoom in" iron-icon="pdf:add" role="button" tabindex="0" aria-disabled="false"> </cr-icon-button> </span> <span class="vertical-separator"></span> <cr-icon-button id="fit" title="Fit to page" aria-label="Fit to page" iron-icon="pdf:fit-to-height" role="button" tabindex="0" aria-disabled="false"> </cr-icon-button> <cr-icon-button id="rotate" suppress-rtl-flip="" aria-label="Rotate counterclockwise" title="Rotate counterclockwise" iron-icon="pdf:rotate-left" role="button" tabindex="0" aria-disabled="false"> </cr-icon-button> <!--?lit$02748104126$--> </div> <div id="end"> <viewer-download-controls id="downloads"> </viewer-download-controls> <cr-icon-button id="print" title="Print" aria-label="Print" iron-icon="cr:print" role="button" tabindex="0" aria-disabled="false"> </cr-icon-button> <cr-icon-button id="more" title="More actions" aria-label="More actions" iron-icon="cr:more-vert" role="button" tabindex="0" aria-disabled="false"></cr-icon-button> </div> </div> </viewer-toolbar> </body>
我写的C#代码尝试定位并点击下载按钮,代码如下:
frameElement = WebElementFinder.GetElement(Driver, XPATHOFFrame); Driver.SwitchTo().Frame(frameElement); IWebElement elementInsideIframe = Driver.FindElement(By.TagName("embed")); IWebElement shadowHost1 = Driver.FindElement(By.Id("viewer")); ISearchContext shadowRoot1 = shadowHost1.GetShadowRoot(); IWebElement shadowHost2 = shadowRoot1.FindElement(By.Id("toolbar")); ISearchContext shadowRoot2 = shadowHost2.GetShadowRoot(); IWebElement shadowHost3 = Driver.FindElement(By.Id("downloads")); ISearchContext shadowRoot3 = shadowHost3.GetShadowRoot(); IWebElement shadowHost4 = Driver.FindElement(By.Id("download")); ISearchContext shadowRoot4 = shadowHost3.GetShadowRoot(); shadowRoot4.FindElement(By.Id("icon")).Click();
但运行时抛出了异常:
"no such element: Unable to locate element: {"method":"css selector","selector":"#viewer"
想问问大家,这个问题出在哪?怎么才能正确定位到PDF查看器里的下载按钮呢?麻烦大家帮忙指点一下,谢谢啦!
备注:内容来源于stack exchange,提问作者ayushi
相关产品推荐
相关产品推荐

