I'm trying to download an image which changes on every load.
The src
attribute on the img
tag is constant, though the image being displayed changes (I'm guessing the web app changes the image at src
on every hit).
I did try downloading using src
, and the image returned is different, as expected.
My requirement is to download the image that is displayed on the page, at the moment. I also tried right click to save, but right click is disabled in the page. Any ideas ? I'm using selenium webdriver.Other options are also welcome.
This is what I tried using the src
attribute :
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);
}
As I said, this code works, but I get a different image as the web app changes it on every hit. I need the one that is displayed on the page.
Here's the HTML:
<td width="20%" align="center" class="style1">characters
<font color="#FF0000">*</font>
<img id="regImg" src="../../**/**.php" alt="captcha image" height="25">
</td>
If you don't have the option to regenerate the same image e.g. by appending a request parameter in your test, you can take a screenshot
File sreenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
and extract the image from the screenshot, which should be easy if you know what you are looking for.
Alternatively as per this answer you could configure Selenium driver to use a known directory to store the browser data with:
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
driver = new ChromeDriver(options);
and perhaps the random image will be saved on the disk somewhere under chromeProfilePath
. It's possible but the process is complex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With