Uploading is not working in selenium using Robot in Chrome Broswer - java

I am using the Robot class to upload a file but it's not working.
File explorer is appearing for uploading but the Robot class is not working:
driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/automation-practice-form");
driver.manage().window().maximize();
driver.findElement(By.id("photo")).click;
Thread.sleep(2000);
StringSelection stringSelection = new StringSelection("C:\\Users\\Desktop\\Bug\\ui_1.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
How ever Robot is working fine using chrome browser. Facing issue with Firefox. Is there any solution available?

You can actually avoid using Robot and solve it with selenium only. Just send keys to the file input and then submit the form:
WebElement photo = driver.findElement(By.id("photo"));
photo.sendKeys("C:\\Users\\Desktop\\Bug\\ui_1.png");
// TODO: fill out other fields
// this would find the corresponding form and submit it
photo.submit();

Wait for few seconds before hitting enter. Add robot.delay(3000); in your code as follows. This might solve the problem.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Related

photo upload using Robot class in java

I use a mac machine and i am trying to automate photo upload scenario.
This is the step:
1. Click on "Add photo" element in a webpage
2. It will bring up the system dialogue box to select
3. Once I select a photo in the system dialogue box (either double click the image or single click and click on "open" button ) then
4. I will see a pop up from the web page which shows the preview of the selected photo.
5.In that Pop-up I click on the "Upload" button.
The Robot code I use selects the image from my desktop, but does't do any action after that.
Any suggestions? Or any better way to do this?
public photo_upload_page clickOnAddphoto() throws Exception {
waitAndClickElement(addPhoto);
Thread.sleep(1000);
File file = new File("/Users/mohand/Desktop/Defect.jpg");
StringSelection stringSelection = new StringSelection(file.getAbsolutePath());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(500);
//Open Goto window
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_G);
//Paste the clipboard value
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_V);
//Press Enter key to close the Goto window and Upload window
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(500);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(1000);
waitAndClickElement(buttonUploadPhoto);
return new photo_upload_page();
}
}
Ok I just figured it out. I just added the below code couple of times after steps 4 and it worked!
robot.keyPress(KeyEvent.VK_ENTER);

Robot Class is not working for file upload in IE 11 in Selenium webDriver

I am trying to upload a file in selenium webdriver using java in IE11. Below code is clicking on Browse button but it is not entering or pasting the file name to be uploaded on the newly opened window. It just stucks and nothing happens. Not able to debug the code also.Seems that Robot Class is not responding.
I also tried Send keys also but the behaviour is not consistent.
<input name="ctl00$PlaceHolderMain$UploadDocumentSection$ctl05$InputFile" title="Choose a file" class="ms-fileinput ms-fullWidth" id="ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile" onfocus="ResetSpFormOnSubmitCalled();" onchange="CheckAssetLibMediaExtension()" type="file" size="35">
driver.manage().window().maximize(); WebElement element12 = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(Main.newdocument(driver))); Main.newdocument(driver).click(); Thread.sleep(500); element12 = driver.findElement(By.xpath("//iframe[#class='ms-dlgFrame']")); driver.switchTo().frame(element12);
Thread.sleep(2000);
WebElement element = driver.findElement(By.xpath("//input[#type='file']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Robot robot = new Robot();
StringSelection sel = new StringSelection("C:\\Users\\m9kuil\\Desktop\\ImportAttendeeTemplate.xlsx");
// Copy to clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// Release CTRL+V
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
//Press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
Try once by writing robot.delay(10000); before comment //Press Enter
Other simple alternative for uploading file without robot class :
driver.findElement(By.xpath(".//input[#type='file']")).sendKeys("C:\\Users\\m9kuil\\Desktop\\ImportAttendeeTemplate.xlsx");
wait(10000)

How to upload the file using Junit Selenium?

I tried to upload the file using JUNIT_Selenium.
WebElement fileInfo = driver.findElement(By.xpath("//input[#type='file']"));
fileInfo.sendKeys('my file root');
But, the popup window was not closed in this way :(
Cloud you please give me the answer to solve this problem?
(browser used : Chrome)
I have used some special library Robot.
details.ClickChooseFile();
StringSelection ss = new StringSelection("C:\\Your File");
waitmethod.Waitsec();
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
waitmethod.Waitsec();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
details.ClickSubmitEmpl();

Clicking on a link opens a window browser to save a Document, how to set it in way where there's no pop up and can be stored to a specified location?

When clicking on a link which opens a window browser to save a Document, how to set it in way where there's no window pop up browser being displayed and it can be stored to a specified location?
I would need this for Chrome browser, IE and Mozilla.
This is what I have tried for Chrome:
String downloadFilepath = System.getProperty("user.dir")+"\\PDF Files\\";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("plugins.plugins_disabled", new String[] {"Adobe Flash Player", "Chrome PDF Viewer"});
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
GlobalVars.driver = new ChromeDriver(cap);
But it doesnt seem to be working. It still opens the Windows browser pop up and asks for the location.
Even though I was not able to send it through chrome driver, I handled it through ROBOT API. Here's the code the I have written to handle after the browser pops up:
String Projectpath=System.getProperty("user.dir");
System.out.println(Projectpath);
StringSelection ss= new StringSelection(Projectpath+"\\PDFFiles\\");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
try{
Thread.sleep(3000);
//Perform native keystrokes for Ctrl+V and ENTER Keys
Robot robot = new Robot();
/*robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);*/
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Using selenium with ajax

While uploading file in http://pdftableconverter.com/ with selenium
I have a problem to download file converted
to download xls file I need to click on the Like button which found in ajax web page check link to open image to see the problem
https://drive.google.com/file/d/0B4cxDnPAjctLOFcyZ29xeENHSG8/view?usp=sharing
My problem i need to click the like button to download the xls file
WebDriver driver = new FirefoxDriver();
driver.get("http://pdftableconverter.com/");
File file=null;
try {
file = new File(SeleniumProg.class.getClassLoader().getResource("21.pdf").toURI());
Assert.assertTrue(file.exists());
System.out.println("File Exited");
driver.findElement(By.name("userfile")).sendKeys(file.getAbsolutePath());
WebElement browseButton = driver.findElement(By.id("uploadButton"));
new Actions(driver).click(browseButton).perform();
//put path to your image in a clipboard
StringSelection ss = new StringSelection(file.getAbsolutePath());
//To Clear User Selection
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
//imitate mouse events like ENTER, CTRL+C, CTRL+V
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
WebElement d = driver.findElement(By.id("email"));
d.sendKeys("osama70087#gmail.com");
driver.findElement(By.id("submit_form")).click();
System.out.println("kkkkkkkk");

Categories