如何使用Java检查链接是否为下载链接?
我正在使用selenium从中获取链接,我想检查它是否是下载链接 为此,我使用了使用URL和URLConnection生成的代码:如何使用Java检查链接是否为下载链接?,java,selenium,docker,url,selenium-webdriver,Java,Selenium,Docker,Url,Selenium Webdriver,我正在使用selenium从中获取链接,我想检查它是否是下载链接 为此,我使用了使用URL和URLConnection生成的代码: final WebElement element = driver.findElement(By.xpath(pathToFile)); URL url = null; final String urlFileToDownload = element.getAttribute("href"); UR
final WebElement element = driver.findElement(By.xpath(pathToFile));
URL url = null;
final String urlFileToDownload = element.getAttribute("href");
URLConnection myCon = null;
String contentDisposition = "";
try {
url = new URL(urlFileToDownload);
myCon = url.openConnection();
contentDisposition = myCon.getHeaderField("Content-Disposition");
if (!contentDisposition.contains("attachment;filename=")) {
assertTrue(false, "The link isn't a download link.");
}
} catch (final MalformedURLException e) {
throw new TestIntegrationException("Error while creating URL : " + e.getMessage());
} catch (final IOException e) {
throw new TestIntegrationException("Error while connecting to the URL : " + e.getMessage());
}
assertTrue(true, "Link is a download link.");
try {
url = new URL(urlFichierATelecharger);
myCon = url.openConnection();
myCon.setRequestProperty("Cookie", cookieCible);
contentDisposition = myCon.getHeaderField("Content-Disposition");
if (!contentDisposition.contains("attachment;filename=")) {
assertTrue(false, "The link isn't a download link.");
}
} catch [...]
问题是我的链接是一个下载链接,你可以在这张图片上看到:。(图为控制台的打印屏幕)
当我打开url.openConnection()的连接时代码>
myCon.getHeaderField(“内容处置”)
为空
我已经搜索了一种方法来实现这一点,但每次我的标题字段为空,我都找不到问题,因为当我检查控制台时,我的标题字段不是空的
编辑:我将在docker服务器上启动selenium测试,我认为这是一个需要了解的重要问题。尝试以下方法:
driver.get("https://i.stack.imgur.com/64qFG.png");
WebElement img = wait5s.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/img")));
Dimension h = img.getSize();
Assert.assertNotEquals(0, h);
与其寻找附件,不如看看MIME类型
String contentType = myCon.getContentType();
if(contentType.startsWith("text/")) {
assertTrue("The link isn't a download link.", false);
}
我的问题是由与url.openConnection()
不同的会话引起的
为了解决这个问题,我使用selenium收集了cookie JSESSION,如下所示:
String cookieTarget = null;
for (final Cookie cookie : this.kSupTestCase.getDriver().manage().getCookies()) {
if (StringUtils.equalsIgnoreCase(cookie.getName(), "JSESSIONID")) {
cookieTarget = cookie.getName() + "=" + cookie.getValue();
break;
}
}
然后我将cookie放入打开的连接:
final WebElement element = driver.findElement(By.xpath(pathToFile));
URL url = null;
final String urlFileToDownload = element.getAttribute("href");
URLConnection myCon = null;
String contentDisposition = "";
try {
url = new URL(urlFileToDownload);
myCon = url.openConnection();
contentDisposition = myCon.getHeaderField("Content-Disposition");
if (!contentDisposition.contains("attachment;filename=")) {
assertTrue(false, "The link isn't a download link.");
}
} catch (final MalformedURLException e) {
throw new TestIntegrationException("Error while creating URL : " + e.getMessage());
} catch (final IOException e) {
throw new TestIntegrationException("Error while connecting to the URL : " + e.getMessage());
}
assertTrue(true, "Link is a download link.");
try {
url = new URL(urlFichierATelecharger);
myCon = url.openConnection();
myCon.setRequestProperty("Cookie", cookieCible);
contentDisposition = myCon.getHeaderField("Content-Disposition");
if (!contentDisposition.contains("attachment;filename=")) {
assertTrue(false, "The link isn't a download link.");
}
} catch [...]
这样,我得到了良好的会话,我的URL被识别为下载链接。我共享的图像不是我尝试测试的链接,它是一个链接,当我启动下载时显示我链接的chrome控制台,就像java do when i openConnection()。我得到了它,只是将图像用作目标文件。谢谢你的回答。它仍然返回空元素。当我尝试使用其他链接时,我的函数会工作,因此我认为问题不在函数中,而在selenium或docker中。是否可能该链接不是真正的链接,而是JavaScript看起来像链接?所以它实际上并没有指向任何东西,而是启动了一个动态流。我已经找到了问题的解决方案,你可以在下面看到。问题是由使用openConnection()启动的会话引起的,该会话不是在selenium上运行的会话。