Robot.mouseWheel doesn't work using Java - java

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);
}

Related

Clipboard in a loop and only get the Text first time

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);

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)

Uploading is not working in selenium using Robot in Chrome Broswer

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);

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