我正在尝试将 pdf 嵌入到 html 文档中。 https://pdfobject.com/static.html 这是引用站点。
在 <embed>
页面上使用 HTML
标签嵌入了 pdf 文档
我有以下事情-
Version 55.0.2883.87 m
)并检查该 <embed>
标签上的元素或单击 F12
然后检查相同的标签内容,它显示为 -<embed src="chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/content/web/viewer.html?file=https%3A%2F%2Fpdfobject.com%2Fpdf%2Fsample-3pp.pdf#page=2" type="text/html" width="100%" height="100%">
embed
标签 htmlSystem.setProperty("webdriver.chrome.driver", "D:\\Application\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://pdfobject.com/static.html");
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
driver.switchTo().frame(1);
String tagdata = driver.findElement(By.id("plugin")).getAttribute("outerHTML");
System.out.println(tagdata);
它检索结果为 -
<embed width="100%" height="100%" name="plugin" id="plugin" src="https://pdfobject.com/pdf/sample-3pp.pdf#page=2" type="application/pdf" internalinstanceid="9">
但是在
chrome
打开的 chromedriver.exe
中,如果我执行 F12
并检查嵌入,它会显示与上述结果相同的结果,但是如果我右键单击该 embed
标签,然后单击 inspect element
,它会打开新的开发人员工具控制台,在那里我看到 <embed>
标签为 -<embed id="plugin" type="application/x-google-chrome-pdf" src="https://pdfobject.com/pdf/sample-3pp.pdf#page=2" stream-url="blob:chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/e8337a7a-5af1-456c-8f7a-d8132c67fe6d" headers="Accept-Ranges: bytes
Cache-Control: max-age=2592000
Content-Length: 49672
Content-Type: application/pdf
Date: Wed, 25 Jan 2017 04:51:36 GMT
Expires: Fri, 24 Feb 2017 04:51:36 GMT
Last-Modified: Sat, 19 Mar 2016 06:18:44 GMT
MS-Author-Via: DAV
Server: Apache
X-Content-Type-Options: nosniff
" background-color="0xFF525659" top-toolbar-height="56" top-level-url="https://pdfobject.com/static.html">
同样的快照是 -
我在这里有几个问题 -
chrome
和 chromedriver.exe
打开的 chrome
检查元素的变化? embedded
html 吗? 最佳答案
你好像有 PDF Viewer Chrome extension如果您手动运行 Chrome,则安装它会执行 PDF 渲染。但是,如果 Chrome 是由 chromedriver 启动的,它会在没有任何浏览器扩展程序的情况下运行,这就是为什么在这种情况下您会获得 Chrome 的默认行为(即自行呈现 PDF)。
如果您确实需要验证 PDF 是否使用该特定扩展名加载和呈现,那么您也可以在 Chrome 由 chromedriver 启动时使用以下方法加载扩展名(代码取自 here):
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
请参阅此处了解如何获取扩展程序的 crx 文件:Location of CRX in chrome after installation?
关于java - 有没有办法在 selenium webdriver 中获取 <embed> 标签资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41844529/