Selenium Webdriver Right-click Save as download file (combined with robot and autoIt)

Source: Internet
Author: User
Tags xpath

First of all, thank Lakshay Sharma the great God for his guidance

Recently I have been studying the selenium Webdriver right-click menu and found that selenium Webdriver cannot manipulate the browser right-click menu.

If I want to save the right button, I can't do it at all.

There are some code to see Webdriver Right click option on the Internet, which is not available with discovery.

New= driver.findelement (By.id ("XPath")); act.movetoelement (link). Contextclick (). SendKeys ( Keys.arrowsdown). Build (). Perform ();

There is no way to get the right-click menu using Actions.

Later posted in a forum, an Indian expert gave out solution, perfect! Perfect solution

http://forumsqa.com/question/how-to-click-the-option-of-the-menu-which-the-right-click-pop-up/

The scheme is as follows:

1.selenium Popup Right-click menu

2.robot Select the relevant menu

3. Call AutoIt to implement the Windows GUI save operation

Tips

Visual AutoIt can not operate the web elements, such as I am currently using AutoIt to get rich text box, but can't get the relevant classs, get only the browser information

Don't say much nonsense, test case is as follows

1. Open AutoIt's website

2.click download page

3. Select the AutoIt download icon and right-click Save As

4. Enter the specified path in the popup Save As window, click Save

If you have Selenium Foundation, it is easy. Bring up the right-click menu only requires the Contexclick method of action

Action.contextclick (myelement). Build (). Perform ();

The next step is to select Save As on the right-click menu

Use robot, simulate keyboard operation, use arrow keys

New// This would bring the selection down one by one robot.keypress (Keyevent.vk_down) ; Thread.Sleep (+); Robot.keypress (Keyevent.vk_down); Thread.Sleep (+); Robot.keypress (Keyevent.vk_down); Thread.Sleep (+); Robot.keypress (Keyevent.vk_down); Thread.Sleep (+// This was to release the "Down Key", before this enterwon't work 
   
     Robot.keyrelease (Keyevent.vk_down); Thread.Sleep (+
    ); Robot.keypress (keyevent.vk_enter);
   

It's time to hand it over to AutoIt. Process Save As window

AutoIt How to use:

Locate the Save button, use the Controlfocus method, locate the edit box (filename) title is "Save As", class is edit, instance is 1

Then use the Controlsettext method to enter the save path, locate the Save button, and use the Controlclick method to click the Save button

Controlfocus ("Save As","","Edit1"); Controlfocus ("title","text", ControlID) Edit1=edit instance1; WaitTenSeconds forThe Upload window toappear winwait ("[CLASS: #32770]","",Ten); Set inputFocus toThe Edit control ofUpload windowusingThe handle returned by Winwait Controlfocus ("Save As","","Edit1") Sleep ( -); SetThe File name text onThe Edit field Controlsettext ("Save As","","Edit1","D:\autoit-v3-setup") Sleep ( -); Click onThe Open button Controlclick ("Save As","","Button1");

Then use AutoIt to convert to exe format executable file

Using the Java Runtime class call

Runtime.getruntime (). EXEC ("E:\\test\\download.exe");

The full code is as follows:

 PackageCom.packt.webdriver.chapter2;Importjava.awt.AWTException;ImportJava.awt.Robot;Importjava.awt.event.KeyEvent;ImportJava.io.File;Importjava.io.IOException;Importjava.util.List;ImportJava.util.concurrent.TimeUnit;Importorg.apache.commons.io.FileUtils;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.JavascriptExecutor;ImportOrg.openqa.selenium.Keys;ImportOrg.openqa.selenium.OutputType;ImportOrg.openqa.selenium.TakesScreenshot;ImportOrg.openqa.selenium.WebDriver;//import org.openqa.selenium.WebDriver.Navigation;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.chrome.ChromeDriver;Importorg.openqa.selenium.chrome.ChromeOptions;ImportOrg.openqa.selenium.firefox.FirefoxDriver;Importorg.openqa.selenium.interactions.Actions;ImportCom.thoughtworks.selenium.Selenium;ImportCom.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;  Public classAutoitdownload { Public Static voidMain (String [] args)throwsinterruptedexception, awtexception {String URL= "Https://www.autoitscript.com"; //Avoid Chrome warnning message like "Unsupported command-line flag--ignore-certificate-errors."Chromeoptions options =Newchromeoptions (); Options.addarguments ("--test-type"); System.setproperty ("Webdriver.chrome.driver", "E:\\chromedriver.exe"); Webdriver Driver=Newchromedriver (options); //Webdriver Driver = new Firefoxdriver ();Driver.get (URL);         Driver.manage (). window (). Maximize (); Driver.manage (). Timeouts (). Pageloadtimeout (30, Timeunit.seconds); Driver.manage (). Timeouts (). Implicitlywait (30, Timeunit.seconds); Webelement Editor=driver.findelement (By.xpath ("//*[@id = ' menu-item-207 ']")); Actions Actions=NewActions (driver);        Actions.movetoelement (editor). Perform (); //Locate Download LinkWebelement d=driver.findelement (By.xpath ("//*[@id = ' menu-item-209 ']/a"));               D.click (); Thread.Sleep (5000); //Right click the download link//Locate Download Link//Right click the download linkWebelement download=driver.findelement (By.xpath ("//img[starts-with (@alt, ' Download AutoIt ')]);//*[@id = "Content-area"]/div/table/tbody/tr[1]/td[2]/p/a/imgJavascriptexecutor js=(javascriptexecutor) driver; //Roll down and keep the element to the center of browserJs.executescript ("Arguments[0].scrollintoview (true);", download);        Actions.movetoelement (download). Contextclick (). Build (). Perform (); Robot Robot=NewRobot (); //This would bring the selection down one by onerobot.keypress (Keyevent.vk_down); Thread.Sleep (1000);           Robot.keypress (Keyevent.vk_down); Thread.Sleep (1000);           Robot.keypress (Keyevent.vk_down); Thread.Sleep (1000);           Robot.keypress (Keyevent.vk_down); Thread.Sleep (1000); //robot.keypress (Keyevent.vk_down); //Thread.Sleep (1000); //This was to release the "Down Key", before this enter won't workrobot.keyrelease (Keyevent.vk_down); Thread.Sleep (1000);                       Robot.keypress (Keyevent.vk_enter); //This code block would snapshot the browserFile scrshot=NewFile ("D:\\1.png"); File Scrfile=((takesscreenshot) driver). Getscreenshotas (Outputtype.file); Try{fileutils.copyfile (scrfile, scrshot); } Catch(IOException e) {//TODO auto-generated Catch blockSystem.out.println ("Can ' t Save screenshot");        E.printstacktrace (); }         finally{System.out.println ("Screen shot finished"); }       //System.out.println (Scrfile.getabsolutepath ()); //Call autoIt to save the file        Try{runtime.getruntime (). EXEC ("E:\\test\\download.exe"); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } thread.sleep (150000);            Driver.quit (); }    }

Selenium Webdriver Right-click Save as download file (combined with robot and autoIt)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.