Friday, March 24, 2023

Automating Image Upload using Selenium WebDriver: A Step-by-Step Guide

 package Practice;

import java.awt.AWTException;

import java.awt.Robot;

import java.awt.Toolkit;

import java.awt.datatransfer.StringSelection;

import java.awt.event.KeyEvent;

import java.time.Duration;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class Get_Photo {

@SuppressWarnings("deprecation")

public static void main(String[] args) throws InterruptedException, AWTException {

System.setProperty ("webdriver.chrome.driver", "D:\\SURIYA\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));

driver.get("http://localhost:3000/SignUp");

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// identify image

WebElement ph = driver.findElement(By.xpath("//button[normalize-space()='photo']"));

ph.click();

Thread.sleep(2000);

Robot rb = new Robot();

StringSelection str = new StringSelection("C:\\Users\\Admin\\Surya.jpg");

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null);

// press Contol+V for pasting

rb.keyPress(KeyEvent.VK_CONTROL);

rb.keyPress(KeyEvent.VK_V);

// release Contol+V for pasting

rb.keyRelease(KeyEvent.VK_CONTROL);

rb.keyRelease(KeyEvent.VK_V);

// for pressing and releasing Enter

rb.keyPress(KeyEvent.VK_ENTER);

rb.keyRelease(KeyEvent.VK_ENTER);

Thread.sleep(3000);

}

}

No comments:

Post a Comment