以编程方式在网页上下载动态图像

Nee*_*raj 11 java selenium selenium-webdriver

我正在尝试下载每次加载时都会发生变化的图像.在src该属性img标记是恒定的,但所显示的图像的变化(我猜的Web应用程序在改变图像src上的每一击).

我尝试下载使用src,并返回的图像是不同的,如预期的那样.

我的要求是下载页面上显示的图像.我也尝试右键单击保存,但在页面中禁用了右键单击.有任何想法吗 ?我正在使用selenium webdriver.Other选项也很受欢迎.

这是我尝试使用该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);
}
Run Code Online (Sandbox Code Playgroud)

正如我所说,这段代码有效,但我得到了一个不同的图像,因为网络应用程序在每次点击时都会更改它.我需要页面上显示的那个.

这是HTML:

<td width="20%" align="center" class="style1">characters
    <font color="#FF0000">*</font>
    <img id="regImg" src="../../**/**.php" alt="captcha image" height="25">
</td>
Run Code Online (Sandbox Code Playgroud)

Kar*_*cki 3

如果您无法选择重新生成相同的图像,例如通过在测试中附加请求参数,您可以截取屏幕截图

File sreenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Run Code Online (Sandbox Code Playgroud)

并从屏幕截图中提取图像,如果您知道自己在寻找什么,这应该很容易。

或者,根据此答案,您可以将 Selenium 驱动程序配置为使用已知目录来存储浏览器数据:

ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
driver = new ChromeDriver(options);
Run Code Online (Sandbox Code Playgroud)

也许随机图像将保存在磁盘上的某个位置chromeProfilePath有可能,但过程很复杂