To upload a picture using Selenium API, I have written the following lines of code where I am making a use of Robot class instead of using Auto IT because Auto IT does not work on mac machine except windows.
In the below example I have used the following link where I am able to upload the pic using Robot class successfully.
https://www.google.com/local/place/rap/edit/hoursv2?sa=X&ved=2ahUKEwi1mr-lt-L2AhXdxL4IHTZfCtkQ_s8EegQIABAH&isEmbedded=true
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.chrome.ChromeOptions;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
public class Query
{
static WebDriver driver;
public static void main(String args[]) throws AWTException
{
WebDriverManager.chromedriver().setup();
ChromeOptions options=new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
driver=new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().deleteAllCookies();
driver.get("https://www.google.com/local/place/rap/edit/hoursv2?sa=X&ved=2ahUKEwi1mr-lt-L2AhXdxL4IHTZfCtkQ_s8EegQIABAH&isEmbedded=true");
driver.findElement(By.xpath("//span[contains(text(),'Add a photo')]")).click();
Robot robot = new Robot();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection("C:\\Users\\StapleAutomation\\OneDrive\\Desktop\\B1.jpg");
clipboard.setContents(stringSelection, null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
//Simulate Enter key event
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}
********************************************************************************************************
No comments:
Post a Comment