Selenium Web Driver Implementation screenshot function "Go"

Source: Internet
Author: User
Tags xpath

When verifying certain critical steps, you need to take a screenshot to record the situation.

Webdriver, you need to introduce

Import Java.io.file;import Java.io.ioexception;import Org.apache.commons.io.fileutils;import Org.openqa.selenium.outputtype;import Org.openqa.selenium.TakesScreenshot;

Method

public static void Snapshot (Takesscreenshot drivername, String filename)  {      //This method would take a screen shot, R Equire-parameters, one is driver name, and another is file name          String Currentpath = System.getproperty ("User.dir"); /get Current Work folder    System.out.println (Currentpath);    File scrfile = Drivername.getscreenshotas (outputtype.file);        Now the can do whatever you need to does with it, for example copy somewhere        try {            System.out.println ("Save snaps Hot path is: "+currentpath+"/"+filename";            Fileutils.copyfile (Scrfile, New File (currentpath+ "\ \" +filename));        catch (IOException e) {            //TODO auto-generated catch block            System.out.println ("Can ' t Save screenshot");            E.printstacktrace ();        }         Finally        {                       System.out.println ("screen shot finished");        }  }

The following tasks:

1. Use selenium to open Baidu;

2. Enter the Selenium keyword;

3. Search and open the selenium Baidu Encyclopedia;

The specific code is as follows:

  1 package Baidu;  2 3 4 5 import java.io.File;  6 Import java.io.IOException; 7 8 9 Import org.apache.commons.io.FileUtils; Import Org.openqa.selenium.By; Import Org.openqa.selenium.OutputType; Import Org.openqa.selenium.TakesScreenshot; Org.openqa.selenium.WebDriver import; //import org.openqa.selenium.WebDriver.Navigation; + Import org.openqa.selenium.WebElement; Import Org.openqa.selenium.chrome.ChromeDriver; All public class Selenium {snapshot (takesscreenshot drivern, public static void) AME, String filename)-{//This method would take-screen shot, require-parameters, one is driver name, an Other is file name, file Scrfile = Drivername.getscreenshotas (outputtype.file); (//Now) can do whatever your need to does with it, for example copy somewhere try {+ S Ystem.out.println ("Save snapshot Path is:e:/" +filename);          36   Fileutils.copyfile (Scrfile, New File ("e:\\" +filename)); PNS} catch (IOException e) {$//TODO auto-generated catch block System.out.println (" Can ' t save screenshot "); E.printstacktrace ();   The "System.out.println" ("screen shot finished"); 45} 46         } The public static void main (String [] args) throws Interruptedexception 49 {50 51 52 String url= "http://www.baidu.com";  System.setproperty ("Webdriver.chrome.driver", "E:\\chromedriver.exe"); Webdriver Driver = new Chromedriver (); Driver.get (URL);  //max Size the browser driver.manage (). window (). Maximize (); *//Navigation Navigation = Driver.navigate (); Navigation.to (URL); */Thread.Sleep (2000); Snapshot (Takesscreenshot) driver, "open_baidu.png"); //webelement Reg=driver.findeLement (By.name ("Tj_reg")); //reg.click (); //Webelement KeyWord = driver.findelement (By.id ("KW1")); //find the element webelement KeyWord = driver.findelement (By.xpath ("//input[@id = ' kw1 ‘]")); Keyword.clear (); //send Key Words keyword.sendkeys ("Selenium"); Thread.Sleep (3000); Snapshot (Takesscreenshot) driver, "input_keyword.png"); Webelement submit = Driver.findelement (By.id ("SU1")); System.out.println (Submit.getlocation ()); Submit.click (); Bayi//system.out.println (Driver.getwindowhandle ()); Thread.Sleep (5000); SYSTEM.OUT.PRINTLN//(Driver.getpagesource ()); Pagesource=driver.getpagesource String (); SYSTEM.OUT.PRINTLN//(Pagesource); //webelement Link =driver.findelement (By.xpath (Selenium_link));     Webelement Link =driver.findelement (By.xpath ("//*[@id =\" 1\ "]/h3/a")); *[@id = "1"]/h3/a Link.click ();  Thread.Sleep (5000); Driver.switchto (). Window (Driver.getwindowhandles (). ToArray (new string[0]) [1]); 94//get page Title System.out.println (Driver.gettitle ()); Thread.Sleep (5000); //Navigation.back (); 98 Snapshot ((Takesscreenshot) driver, "open_bake.png");           System.out.println (Driver.gettitle () + "\ n" +driver.getcurrenturl ()); 100 101 102 103 Driver.quit (); 104 105 106}107 108}

In Baidu search results, get the elements you want, you can use the browser's viewing elements, through the XPath method to get







Console output:

Starting Chromedriver (v2.9.248315) on port 33834
Save Snapshot Path Is:e:/open_baidu.png
Screen shot finished
Save Snapshot Path Is:e:/input_keyword.png
Screen shot finished
(858, 179)
Selenium_ Baidu Encyclopedia
Save Snapshot Path Is:e:/open_bake.png
Screen shot finished
Selenium_ Baidu Encyclopedia
Http://baike.baidu.com/subview/478050/6464537.htm?fr=aladdin

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.