I have an Error and I don't get it. I am not getting an Exception.
I have to copy a text with the Robot.
// mark the text
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.delay(1000);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
// robot copy the text
robot.keyPress((KeyEvent.VK_CONTROL));
robot.keyPress((KeyEvent.VK_C));
robot.delay(1000);
robot.keyRelease((KeyEvent.VK_C));
robot.keyRelease((KeyEvent.VK_CONTROL));
then I get the Text via Clipboard
txt = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
After that, I want to set the text to the Clipboard, so I can put it in the text field again with the Robot.
StringSelection stringSelection = new StringSelection(txt);
clipboard.setContents(stringSelection, stringSelection);
// robot mark the hole text
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.delay(1000);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
// robot put the text in
robot.keyPress((KeyEvent.VK_CONTROL));
robot.keyPress((KeyEvent.VK_V));
robot.delay(1000);
robot.keyRelease((KeyEvent.VK_V));
robot.keyRelease((KeyEvent.VK_CONTROL));
This is everything in a loop
while (j < liUnderElementList.size()) {}
The first time it works as expected but on the second pass, I get the first copied text and not the new text.
BUT in my Clipboard is the new Text.
If i wait with
TimeUnit.SECONDS.sleep(2)
nothing changes.
Anyone can help me?
I would be very thankfull.
Seems issue with the order of keys, in the way you are performing. Release event should be from last one. Use below one:
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.delay(1000);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
Related
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);
All operations are:
1, click the windows logo to open the "All Program"
2, click "All Program"
3, move mouse up to the program list
4, wheeling down
Robot robot = new Robot();
robot.mouseMove(35,1062); //click windows logo
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(1000);
System.out.println("open windows");
robot.mouseMove(70,947);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
System.out.println("open all program");
robot.delay(1000);
robot.mouseMove(88,510);
System.out.println("move to chrome");
robot.delay(1000);
for(int index = 0; index < 10; index++){
robot.mouseWheel(1);
robot.delay(1000);
}
but the mouseWheel doesn't work.
I tested your code and robot.mouseWheel works correctly.
Its problem may be of not being active window in your situation.
But I recommend use following code to open chrome and search in it:
Robot robot = new Robot();
//-- open start menu
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.delay(1000);
//-- type ch to find chrome
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.delay(1000);
//-- press Enter to Open it
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
//-- type hi to search
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyRelease(KeyEvent.VK_I);
robot.delay(1000);
//-- press Enter to search it
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
To scroll down on programs in windows 10 I wrote the following code and it works Correctly on my computer with 1920x1080 resolution (but using it is not recommended because the manner may differ in various condition):
Robot robot = new Robot();
//-- open start menu
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.delay(1000);
//-- to move on programs
robot.mouseMove(250,700);
//-- scroll down
for(int index = 0; index < 10; index++) {
robot.mouseWheel(1);
robot.delay(1000);
}
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();
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);
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");