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

Source: Internet
Author: User

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.

Actions act = new Actions (driver); Webelement link = 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

Robot Robot = new Robot (); This would bring the selection down one by one 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 would not work robot.keyrelease (Keyevent.vk_down); Thread.Sleep (1000); 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 instance 1; Wait ten seconds for the Upload window to appear  winwait ("[CLASS: #32770]", "", 10); Set input focus to the edit control of Upload window using the handle returned by winwait  Controlfocus ("Save As", "", "Edit1 ")  Sleep (2000); Set the File name text on the Edit field  controlsettext ("Save As", "" "," Edit1 "," D:\autoit-v3-setup ")  Sleep (2000); Click on the 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:

Package com.packt.webdriver.chapter2; Import Java.awt.awtexception;import java.awt.robot;import Java.awt.event.keyevent;import Java.io.File;import Java.io.IOException; Import Java.util.list;import Java.util.concurrent.timeunit;import org.apache.commons.io.FileUtils; Import Org.openqa.selenium.by;import Org.openqa.selenium.javascriptexecutor;import Org.openqa.selenium.Keys; Import Org.openqa.selenium.outputtype;import Org.openqa.selenium.takesscreenshot;import Org.openqa.selenium.webdriver;//import Org.openqa.selenium.webdriver.navigation;import Org.openqa.selenium.webelement;import Org.openqa.selenium.chrome.chromedriver;import Org.openqa.selenium.chrome.chromeoptions;import Org.openqa.selenium.firefox.firefoxdriver;import Org.openqa.selenium.interactions.actions;import Com.thoughtworks.selenium.selenium;import  Com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium; public class Autoitdownload {public static void main (String [] args) throws Interruptedexception, Awtexception {String url= "https://www.autoitscript.com";        Avoid Chrome warnning message like "Unsupported command-line flag--ignore-certificate-errors."        Chromeoptions options = new Chromeoptions ();               Options.addarguments ("--test-type");          System.setproperty ("Webdriver.chrome.driver", "E:\\chromedriver.exe");         Webdriver Driver = new Chromedriver (options);               Webdriver Driver = new Firefoxdriver ();             Driver.get (URL);         Driver.manage (). window (). Maximize ();        Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds);        Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);        Webelement editor=driver.findelement (By.xpath ("//*[@id = ' menu-item-207 ']");        Actions actions=new actions (driver);        Actions.movetoelement (editor). Perform ();        Locate download link webelement 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 li NK Webelement Download=driver.findelement (By.xpath ("//img[starts-with (@alt, ' Download AutoIt ')]");//*[@id = "        Content-area "]/div/table/tbody/tr[1]/td[2]/p/a/img javascriptexecutor js= (javascriptexecutor) driver; Roll down and keep the element to the center of browser Js.executescript ("Arguments[0].scrollintoview (true);", D        Ownload);        Actions.movetoelement (download). Contextclick (). Build (). Perform ();             Robot Robot = new Robot ();           This would bring the selection down one by one 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); Robot. keyPress (Keyevent.vk_down);           Thread.Sleep (1000);           This was to release the down key, before this enter would not work robot.keyrelease (Keyevent.vk_down);           Thread.Sleep (1000);                       Robot.keypress (Keyevent.vk_enter);        This code block would snapshot the browser file scrshot=new file ("D:\\1.png");        File scrfile= ((takesscreenshot) driver). Getscreenshotas (Outputtype.file);        try {fileutils.copyfile (scrfile, scrshot); } catch (IOException e) {//TODO auto-generated catch block System.out.println ("Can ' t save Screensh            OT ");        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 block E.printstacktrace ();        } thread.sleep (150000);            Driver.quit (); }    }

Article turned from: http://www.cnblogs.com/tobecrazy/p/3969390.html

Young

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.