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

标签 java selenium selenium-webdriver selenium-chromedriver

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

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

我有以下事情-

  • 如果我打开 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%">
    
  • 但是如果我使用以下脚本来获取 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);
    

    它检索结果为 -
    <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">
    

    同样的快照是 -

    enter image description here

    我在这里有几个问题 -
  • 使用系统安装的 chromechromedriver.exe 打开的 chrome 检查元素的变化?
  • 有没有办法得到chromedriver打开的结果?
  • 有什么办法可以得到 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/

    相关文章:

    java - 为什么我们不能在 C++ 中的同一个类中声明一个类的对象,但在 Java 中却允许这样做?

    css - 如何编写 CSS 表达式以单击 "Select "我尝试过使用不同的 x 路径它在 IE 任何版本中都不起作用

    javascript - 使用 Selenium webdriver 和 Python 获取完全呈现的 HTML

    java - 使用 findelements 时 xpath 定位器出现 "Invalid Selector Used for Locator "错误

    python - 无法解析网页中不同海报的链接

    java - org.openqa.selenium.WebDriverException : unknown error: call function result missing 'value'

    Java,Selenium - 属性读取器类 - 如何将字符串传递给另一个类

    java - JList 仅显示有限的行数

    Java if-then 语句中的变量初始化

    java - 输入空输入时如何跳出循环