我正在尝试下载每次加载都会发生变化的图像。
img
标签上的 src
属性是不变的,尽管显示的图像发生了变化(我猜网络应用程序改变了 src
处的图像每次命中)。
我确实尝试使用 src
下载,返回的图像与预期的不同。
我的要求是下载当前页面上显示的图像。 我也试过右击保存,但是右击在页面中被禁用了。 有任何想法吗 ? 我正在使用 selenium webdriver。也欢迎使用其他选项。
这是我尝试使用 src
属性的结果:
public static void download() {
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://*******.com/");
String url = driver.findElement(By.id("regImg")).getAttribute("src");
// run of the mill code to download the image.
downloadImage(url);
}
正如我所说,这段代码有效,但我得到了一个不同的图像,因为网络应用程序在每次点击时都会改变它。我需要页面上显示的那个。
这是 HTML:
<td width="20%" align="center" class="style1">characters
<font color="#FF0000">*</font>
<img id="regImg" src="../../**/**.php" alt="captcha image" height="25">
</td>
最佳答案
如果您没有重新生成相同图像的选项,例如通过在测试中附加请求参数,您可以截屏
File sreenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
并从屏幕截图中提取图像,如果您知道要查找的内容,这应该很容易。
或者根据 this answer您可以将 Selenium 驱动程序配置为使用已知目录来存储浏览器数据:
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
driver = new ChromeDriver(options);
也许随机图像将保存在磁盘上 chromeProfilePath
下的某处。 It's possible but the process is complex .
关于java - 以编程方式在网页上下载动态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51188569/