我很难在“嵌入”webelement中获取某些属性的值。我在页面中跟随WebElement,在浏览器中显示PDF:
<embed id="plugin" type="application/x-google-chrome-pdf" src="https://A-link.dk/plan/53--4c-c3-d5cd72ba/printGenerate.do?target=ON_RESIDENT&format=PDF&saveAsDocument=false&printSelectionId=&_=49365165116" stream-url="blob:chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/95052284-bc11-44c9-a261-6000f48a75d4"
headers="Cache-Control:
Connection: Keep-Alive
Content-Disposition: inline; filename="planDto.pdf";
Content-Type: application/pdf
Date: Mon, 23 Jan 2017 14:47:47 GMT
Keep-Alive: timeout=5, max=90
Pragma: public
Server: Apache/2.4.6 (CentOS)
Transfer-Encoding: chunked
" background-color="0xFF525659" top-toolbar-height="56" full-frame="">
我正在尝试使用selenium chrome webdriver获取headers属性字符串。 使用Chrome开发者控制台,我可以使用
检索值document.getElementsByTagName("embed")[0].getAttribute("headers")
但在selenium java中我尝试这个:
String header = (String) ((JavascriptExecutor) driver.getDriver())
.executeScript("return document.getElementsByTagName(\"embed\")[0].getAttribute(\"headers\");");
我得到一个空字符串。 除此之外,我也尝试过:
WebElement img = driver.findElement(By.tagName("embed"));
String header = img.getAttribute("headers");
但是这会返回NULL。
当我尝试使用
获取src属性时,它工作正常String src = (String) ((JavascriptExecutor) driver.getDriver())
.executeScript("return document.getElementsByTagName(\"embed\")[0].getAttribute(\"src\");");
作为奖励,我可以说我只对文件名感兴趣,所以如果有更好/更简单的解决方案,请赐教。 提前谢谢。
UPDATE V1 使用谷歌开发人员工具,我可以复制此源代码
<html dir="ltr" lang="da"><head>
<base href="chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/">
<meta charset="utf-8">
<link rel="import" href="elements/viewer-error-screen/viewer-error-screen.html">
<link rel="import" href="elements/viewer-page-indicator/viewer-page-indicator.html">
<link rel="import" href="elements/viewer-page-selector/viewer-page-selector.html">
<link rel="import" href="elements/viewer-password-screen/viewer-password-screen.html">
<link rel="import" href="elements/viewer-pdf-toolbar/viewer-pdf-toolbar.html">
<link rel="import" href="elements/viewer-zoom-toolbar/viewer-zoom-toolbar.html">
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<link rel="stylesheet" href="chrome://resources/css/roboto.css">
<link rel="stylesheet" href="index.css">
<title>Print</title></head>
<body>
<viewer-pdf-toolbar id="toolbar" style="transform-origin: 50% 0px 0px; transform: none;"></viewer-pdf-toolbar>
<div id="sizer" style="width: 773px; height: 2239px;"></div>
<viewer-password-screen id="password-screen"></viewer-password-screen>
<viewer-zoom-toolbar id="zoom-toolbar" style="right: -8.5px; bottom: 0px;"></viewer-zoom-toolbar>
<viewer-page-indicator id="page-indicator" style="top: 0px; opacity: 0;"></viewer-page-indicator>
<viewer-error-screen id="error-screen"></viewer-error-screen>
<script src="toolbar_manager.js"></script><iron-a11y-announcer></iron-a11y-announcer>
<script src="viewport.js"></script>
<script src="open_pdf_params_parser.js"></script>
<script src="navigator.js"></script>
<script src="viewport_scroller.js"></script>
<script src="zoom_manager.js"></script>
<script src="pdf_scripting_api.js"></script>
<script src="chrome://resources/js/util.js"></script>
<script src="browser_api.js"></script>
<script src="pdf.js"></script>
<script src="main.js"></script>
<embed id="plugin" type="application/x-google-chrome-pdf" src="https://A-link.dk/questionnaire/schemeanswers/669bb12b-b7c2-4223-96d2-1ccca26147ce/printSchemeAnswer.do?reportId=" stream-url="blob:chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/7aeb6fee-140b-4368-8b4b-537a019387de" headers="Connection: Keep-Alive
Content-Disposition: inline; filename="questionnaire.pdf"
Content-Length: 23414
Content-Type: application/pdf
Date: Tue, 24 Jan 2017 13:16:49 GMT
Keep-Alive: timeout=5, max=99
Server: Apache/2.4.6 (CentOS)
" background-color="0xFF525659" top-toolbar-height="56" full-frame=""></body></html>
答案 0 :(得分:0)
你确定这是正确的元素吗? WebElement img = driver.findElement(By.tagName("embed"));
尝试WebElement img = driver.findElement(By.cssSelector("embed#plugin"));
并查看它是否返回null。
答案 1 :(得分:0)
我不知道这可能是您问题的可行解决方案。但是,由于您只对filename
<embed>
下面的代码感兴趣,可能会帮助您做同样的事情 -
String expectedData = null;
// Get the whole embed tag
String tagdata = driver.findElement(By.id("plugin")).getAttribute("outerHTML");
// use regex expression to extract only your required file from the headers attribute
Pattern patern = Pattern.compile("filename=\"(.+)\";");
Matcher match = patern.matcher(tagdata);
if (match.find())
{
expectedData = match.group(1);
}
System.out.println("Result is ="+ expectedData);
说明: - 以上代码将获取<embed>
代码数据,并与regex
中的filename
匹配,并将结果作为预期文件检索