0

I am trying to Save an image file using Actions and Robot class in selenium Java. I right click on the image through contextclick() and then use CTRL+v for the save As image option. However i am unable to access the window dialog box for Save As . I am also unable to debug as the debugging also does not take me to that line so that i can check the issue.

Below is the code:

WebElement imgRightClick=driver.findElement(By.xpath(".//table[contains(@id,'WebPart_twp285729713')]//img")); 
                Actions action= new Actions(driver);
                action.contextClick(imgRightClick).sendKeys(Keys.CONTROL, "v").build().perform();

                Thread.sleep(2000);

                Robot robot = new Robot();  

                robot.keyPress(KeyEvent.VK_H);
                robot.keyRelease(KeyEvent.VK_H);
                Thread.sleep(2000);

                robot.keyPress(KeyEvent.VK_COLON);
                robot.keyRelease(KeyEvent.VK_COLON);

                Thread.sleep(2000);
                robot.keyPress(KeyEvent.VK_BACK_SLASH);
                robot.keyRelease(KeyEvent.VK_BACK_SLASH);

                Thread.sleep(2000);
                robot.keyPress(KeyEvent.VK_ENTER);
                Thread.sleep(3000L); 

Please have a look into the above issue.

juherr
  • 5,640
  • 1
  • 21
  • 63
  • you mean you are getting the "save as" browse file wizard but you are unable to access it? if you attach any screenshot of how it looks it would be easy to help you! – Waman Feb 14 '17 at 06:05
  • Thanks for your response. I might not be able to attach any screenshot in comments. However, when we right click on some image, we get various options in which one of them is "Save Image As" , and then u get a Save as Dialog box(Windows). Now through selenium i have successfully clicked on the right click option "Save Image As" and the dailog box for save as also get opened. However i am unable to access the "Filename" and the save button on the dialog box, so that i can pass the location where i need to save my Image file. Could you please provide help. – Nikita Kamthan Feb 15 '17 at 05:12
  • is the file browser not highlighted on clicking save as? where you can enter your path? – Waman Feb 15 '17 at 05:18

2 Answers2

0

The trick to get this done is pretty simple. You manually right click on the image and hit "save as". Now try to enter the file path and "save" button on the wizard just by using your keyboard.

You can switch control to various elements using "TAB" and then "ENTER" to click.

Implement the same keyboard flow using Robot Class!

Hope it helps!

Waman
  • 1,179
  • 7
  • 16
  • Thanks for your response, I have tried this earlier but my code stops executing after this line of code action.contextClick(imgRightClick).sendKeys(Keys.CONTROL, "v").build().perform(); when i tried debugging my further code i.e Robot class was not executing. Please have a look at my code above. Thanks. – Nikita Kamthan Feb 16 '17 at 04:21
  • did you try to focus the image using selenium, but right click or ctrl+v using robot class? – Waman Feb 16 '17 at 04:35
  • through selenium i have reached to the windows save As dialog box, but i am stuck to given it a location to get saved . – Nikita Kamthan Feb 16 '17 at 09:54
  • is the control not switching to the "save as" wizard? automatically? – Waman Feb 16 '17 at 10:27
  • yes.. I am unable to control the Save as wizard.. though i can succeefully open it through selenium. – Nikita Kamthan Feb 16 '17 at 11:05
  • could you try, right click in selenium and then use arrow keys in robot to select "save as" and then proceed from there.. or when your save as wizard opens, try to get control manually by using tab and see if you can get the control to the wizard – Waman Feb 16 '17 at 11:12
  • I tried as you had suggested, but the right click options disappears when i switch to the robot class keyevents, which in turn goes into catch. – Nikita Kamthan Feb 17 '17 at 07:43
  • I wish I could see your screen! it's pretty hard to imagine whats happening. but why does it go into catch? all the selenium code would have executed and robot would never give you an exception. – Waman Feb 17 '17 at 08:46
  • i can share you my piece of code.. so that u get an idea of things happening. Now in the next piece of code my code does not execute after the "Action.contextClick(imgRightClick)..." method, so it never goes to the Robot class. – Nikita Kamthan Feb 17 '17 at 09:20
  • Actions action= new Actions(driver); action.contextClick(imgRightClick).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform(); Robot robot= new Robot(); robot.keyPress(KeyEvent.VK_H); robot.keyRelease(KeyEvent.VK_H); Thread.sleep(2000); robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_COLON); robot.keyRelease(KeyEvent.VK_COLON); robot.keyRelease(KeyEvent.VK_SHIFT); Thread.sleep(2000); robot.keyPress(KeyEvent.VK_BACK_SLASH); – Nikita Kamthan Feb 17 '17 at 09:22
  • robot.keyRelease(KeyEvent.VK_BACK_SLASH); Thread.sleep(2000); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER Thread.sleep(3000L); – Nikita Kamthan Feb 17 '17 at 09:22
  • hey sendKeys wont work there.. You need to do that using robot class.. If we could have a quick discussion it would be good.. instead of chats after a long gap :) – Waman Feb 17 '17 at 09:26
  • And can you not use, findelementbyId/css/name/xpath to click on the image? – Waman Feb 17 '17 at 09:28
  • initially i had tried using robot class, but got no results. so tried using send keys then when it did not work. – Nikita Kamthan Feb 17 '17 at 09:29
  • that image is a web element right?? can we find it by id or css – Waman Feb 17 '17 at 09:29
  • the DOM structure is like this :
    – Nikita Kamthan Feb 17 '17 at 09:31
  • the code i have used above takes to the SaveAs wizard but does not allow me to control it. – Nikita Kamthan Feb 17 '17 at 09:35
  • ignore! try something like this: Robot robot = new Robot(); DriverClass.driver.findElement(By.Css(identifer)).click(); robot.keyPress(KeyEvent.VK_ENTER); – Waman Feb 17 '17 at 09:45
  • try adding print / log statement after every statement in the code before and after right click, so that you can figure out if the robot code was executed or not – Waman Feb 17 '17 at 09:51
  • What doe DriverClass refer to ? – Nikita Kamthan Feb 17 '17 at 09:53
  • ahh, something specific to my code.. I meant driver.findElement(By.Css(identifer)).click – Waman Feb 17 '17 at 09:54
  • Actions action= new Actions(driver); Actions action= new Actions(driver); action.contextClick(productLink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform(); – Waman Feb 17 '17 at 09:58
  • the options in right click are your feature specific or the generic right click options that we get? – Waman Feb 17 '17 at 10:07
0

This is a sample code for downloading a image, which is working fine for me.

import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class Sample {

public static void main(String[] args) throws Exception 
{
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.google.co.in");
    driver.findElement(By.name("q")).sendKeys("selenium");
    Actions action= new Actions(driver);
    action.contextClick(driver.findElement(By.id("logo")).findElement(By.tagName("img"))).build().perform();
    action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
    Thread.sleep(2000);
    action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
    Thread.sleep(2000);
    action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
    Thread.sleep(2000);
    action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
    Thread.sleep(2000);
    action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
    Thread.sleep(2000);
    Robot RoboKey = new Robot();
    RoboKey.keyPress(KeyEvent.VK_W);
    RoboKey.keyPress(KeyEvent.VK_A);
    RoboKey.keyPress(KeyEvent.VK_M);
    RoboKey.keyPress(KeyEvent.VK_A);
    RoboKey.keyPress(KeyEvent.VK_N);
    RoboKey.keyPress(KeyEvent.VK_PERIOD);
    RoboKey.keyPress(KeyEvent.VK_J);
    RoboKey.keyPress(KeyEvent.VK_P);
    RoboKey.keyPress(KeyEvent.VK_G);
    Thread.sleep(2000);
    RoboKey.keyPress(KeyEvent.VK_ENTER);


}
}

Hope it helps!

Waman
  • 1,179
  • 7
  • 16
  • I tried using your code as it is (changing the element id and the number of arrow keys pressed), however i am still not able to get the control of the Save As wizard. I am using selenium 2.42.2 version with 24.7 version of mozilla can this be the issue due to which i am unable to control the wizard. Please suggest. – Nikita Kamthan Feb 20 '17 at 05:03
  • Can you try the same script without even editing anything? so that can we can see if it is issue only in your site or in general? Versions might be problem is well, I am using selenium 2.45 and firefox 43. – Waman Feb 21 '17 at 04:56
  • I have now used your code and the same issue appears while controlling the SaveAs wizard. The code shared by you also faces problem in saving the image. I have to manually click on the Save button which should have been done by selenium. – Nikita Kamthan Feb 21 '17 at 07:04
  • hmm then it is a general issue.. try with the same firefox version..I did face issue while using firefox 51 but now I have downgraded to 37.0 which worked – Waman Feb 21 '17 at 07:11
  • I cannot downgrade my firefox, as this is my requirement. So any other way out to resolve this? – Nikita Kamthan Feb 21 '17 at 07:37
  • you actually need to upgrade as you are using 24.7, there is this problem when using selenium there are errors in a particular versions of firefox for which you have to switch to the stable one in most of the cases! – Waman Feb 21 '17 at 07:38
  • just in case you want same version you can also try: http://stackoverflow.com/questions/31547459/selenium-sendkeys-different-behaviour-for-chrome-firefox-and-safari – Waman Feb 21 '17 at 07:58