Downloading Image from spider web page Is slow but It Is tricky task In selenium WebDriver. Previously I described usages of Actions class of selenium WebDriver In seek out cases to perform tricky actions. You tin bathroom honor all Actions degree examples link on THIS PAGE. nether advanced user
Interaction API tutorial section. For downloading Image, We volition operate same WebDriver Actions degree alongside coffee robot class.
Interaction API tutorial section. For downloading Image, We volition operate same WebDriver Actions degree alongside coffee robot class.
For saving Image from spider web page In selenium webdriver, We convey to perform bellow given actions.
- Right click on Image
- Select "Save Image As" selection from mouse correct click context menu.
- Enter file advert In salve Image dialog
- Press Save button.
Right click on Image using contextClick() method of Actions class
As you lot know, "Save Image As" selection volition display when you lot correct click on whatever Image. We volition operate contextClick() method of WebDriver Actions degree to correct click on Image equally shown In bellow Image.
Code to correct click on Image Is equally bellow.
//Locate Image WebElement Image =driver.findElement(By.xpath("//img[@border='0']")); //Rihgt click on Image using contextClick() method. Actions action= novel Actions(driver); action.contextClick(Image).build().perform();
Select "Save Image As" option
If you lot meet In Image, We tin bathroom choose "Save Image As" selection using CONTROL + V from keyboard. To produce It In selenium webdriver, We volition operate bellow given code.
//To perform press Ctrl + 5 keyboard push action. action.sendKeys(Keys.CONTROL, "v").build().perform();
It volition opened upward Save Image dialog.
Why demand robot class?
As you lot know, Actions degree tin bathroom assistance you lot to perform whatever activity on spider web page only. It tin bathroom non grip whatever windows dialog similar Save Image. We tin bathroom operate Java Robot degree to grip Save Image dialog. Robot degree Is really proficient characteristic of Java using which nosotros tin bathroom post keyboard fundamental press as well as fundamental unloosen events really easily to movement into file advert as well as press Save push on Save Image dialog. Code to movement into file advert "D:\test" as well as press Save push on Save Image dialog Is equally bellow.
Robot robot = novel Robot(); // To press D key. robot.keyPress(KeyEvent.VK_D); // To press : key. robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_SEMICOLON); robot.keyRelease(KeyEvent.VK_SHIFT); // To press \ key. robot.keyPress(KeyEvent.VK_BACK_SLASH); // To press "test" fundamental i yesteryear one. robot.keyPress(KeyEvent.VK_T); robot.keyPress(KeyEvent.VK_E); robot.keyPress(KeyEvent.VK_S); robot.keyPress(KeyEvent.VK_T); // To press Save button. robot.keyPress(KeyEvent.VK_ENTER);
This way, We tin bathroom salve Image using Actions degree of WebDriver as well as Robot degree of java. Full practical illustration Is equally bellow.
package Testing_Pack; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; world degree Actions_Test { WebDriver driver; @BeforeTest world void setup() throws Exception { driver =new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(" "); } @Test world void Save_Image() throws IOException, InterruptedException, AWTException { //Locate Image WebElement Image =driver.findElement(By.xpath("//img[@border='0']")); //Rihgt click on Image using contextClick() method. Actions action= novel Actions(driver); action.contextClick(Image).build().perform(); //To perform press Ctrl + 5 keyboard push action. action.sendKeys(Keys.CONTROL, "v").build().perform(); Thread.sleep(3000); Robot robot = novel Robot(); // To press D key. robot.keyPress(KeyEvent.VK_D); // To press : key. robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_SEMICOLON); robot.keyRelease(KeyEvent.VK_SHIFT); // To press \ key. robot.keyPress(KeyEvent.VK_BACK_SLASH); // To press "test" fundamental i yesteryear one. robot.keyPress(KeyEvent.VK_T); robot.keyPress(KeyEvent.VK_E); robot.keyPress(KeyEvent.VK_S); robot.keyPress(KeyEvent.VK_T); // To press Save button. robot.keyPress(KeyEvent.VK_ENTER); } }
VIEW EXAMPLE of robot degree to unopen browser tabs.