1 import java.awt.Robot; 2 import java.awt.event.KeyEvent; 3 import java.util.concurrent.TimeUnit; 4 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 9 public class TakeScreenshotTest{ 10 11 public static void main(String[] args) throws Exception { 12 //初始化一个 13 System.setProperty("webdriver.chrome.driver", ".\Tools\chromedriver.exe"); 14 WebDriver driver = new ChromeDriver(); 15 16 //最大化浏览器窗口 17 //driver.manage().window().maximize(); 18 19 //设置隐式等待2秒 20 driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); 21 22 //打开百度 23 driver.get("https://www.baidu.com"); 24 driver.findElement(By.xpath("//*/input[@id='kw']")).sendKeys("macaca"); 25 26 Robot rb = new Robot(); 27 28 rb.keyPress(KeyEvent.VK_TAB); 29 rb.keyRelease(KeyEvent.VK_TAB); 30 31 32 //调用截图方法 33 RobotScreen.captureScreenshot(); 34 35 36 } 37 38 39 }