自动化java+webdriver常用的一些脚本

简述 :写自动化常用的一些脚本

(1)设置当前窗口为最大化

driver.manage().window().maximize();

(2)模拟键盘单机

Actions action = new Actions(driver);

action.click(driver.findElement(By.xpath("/HTML/BODY/DIV[2]/FORM/DIV[4]/BUTTON"))).build().perform();

(3)设置睡眠等待时间为3秒

public void logins() throws InterruptedException{

Thread.sleep(3000);

}

(4)给文本赋值

driver.findElement(by.xpath("/html/body/div[3]/div[1]/span/div/ul/li[2]/a")).sendkeys("123456");

(5)鼠标悬浮在元素上

Actions action=new Actions(dr);

action.moveToElement(dr.findElement(By.xpath("/html/body/div[1]/div/div[2]/ul/li[2]/a/span"))).perform();

(6)文件、图片上传

dr.findElement(By.xpath("/html/body/div[3]/div[1]/span/div/ul/li[2]/a")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\图片格式\\jpg\\19.jpg");  //注意这里路径是反斜杠

(7)窗口截图

WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
try {
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile,new File("d:\\screenshot.png")); //指定图片的保存路径及文件名
} catch (Exception e) {
e.printStackTrace();
}
driver.quit();

原文地址:https://www.cnblogs.com/caozq1/p/7804146.html