有没有办法在 selenium webdriver 中获取 <embed> 标签资源

Nar*_*raR 6 java selenium selenium-chromedriver selenium-webdriver

我正在尝试将 pdf 嵌入到 html 文档中。https://pdfobject.com/static.html这是参考站点。

使用页面上的<embed>标签嵌入 pdf 文档HTML

我有以下事情-

  1. 如果我打开 chrome 浏览器(我正在使用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%">
    
    Run Code Online (Sandbox Code Playgroud)
  2. 但是如果我使用以下脚本来获取embed标签 html

    System.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);
    
    Run Code Online (Sandbox Code Playgroud)

    它检索结果为 -

    <embed width="100%" height="100%" name="plugin" id="plugin" src="https://pdfobject.com/pdf/sample-3pp.pdf#page=2" type="application/pdf" internalinstanceid="9">
    
    Run Code Online (Sandbox Code Playgroud)

但是如果我chrome打开并检查嵌入,它会显示与上述结果相同的结果,但是如果我右键单击该标签然后单击,它会打开新的开发人员工具控制台,我看到标签为 -chromedriver.exeF12embedinspect 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">
Run Code Online (Sandbox Code Playgroud)

同样的快照是 -

在此处输入图片说明

我在这里有几个问题 -

  1. 使用安装chromechromedriver.exe打开的系统来检查元素的变化chrome

  2. 有没有办法获得 chromedriver 打开的结果?

  3. 有什么办法可以得到embeddedhtml 吗?

小智 0

您似乎安装了PDF 查看器 Chrome 扩展,如果您手动运行 Chrome,该扩展会执行 PDF 渲染。但是,如果 Chrome 是由 chromedriver 启动的,则它会在没有任何浏览器扩展的情况下运行,这就是为什么在这种情况下您会获得 Chrome 的默认行为(即自行渲染 PDF)。

如果您确实需要验证 PDF 是否已使用该特定扩展加载和呈现,那么您也可以在 chromedriver 启动 Chrome 时使用以下方法加载扩展(代码取自此处):

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);
Run Code Online (Sandbox Code Playgroud)

请参阅此处,了解如何获取扩展程序的 crx 文件:安装后在 chrome 中安装 CRX 的位置?