对于Selenium的基本元素的操作,就自己的了解做了一个基本的介绍,这篇直接上代码,针对一个页面如何操作写了些基本的操作脚本,希望对初学者有一定的帮助,也希望通过这些总结让自己有一些清晰的认识和了解:
Demo文件下载地址:
Demo 的样图:
Java Code:
import java.util.Iterator;import java.util.List;import java.util.Set;import java.util.concurrent.TimeUnit;import org.openqa.selenium.Alert;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.interactions.Actions;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.Select;import org.openqa.selenium.support.ui.WebDriverWait;public class BasicUIExercise { WebDriver driver; public void openBrowser() { System.setProperty("webdriver.chrome.driver", "E:\\JavaCodeWorkSpace\\JProject0610\\ChromeDriver\\chromedriver.exe"); driver = new ChromeDriver(); } // 退出所有浏览器 public void quitBrowser() { driver.quit(); } // 退出当前的单个浏览器 public void closeBrowser() { driver.close(); } public void goToTarget(String URL) { driver.navigate().to(URL); driver.navigate().refresh(); // driver.get(URL); } public void backToTarget() { driver.navigate().back(); } public void waitTime() { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } public void maxWindow() { driver.manage().window().maximize(); } // 拖动页面滚动条到最低部 public void executeJS(String script) { // String js = "var q=document.documentElement.scrollTop=10000"; JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(script); } public void testInput(String value) { WebElement element = driver.findElement(By .xpath(".//*[@id='input']/input")); // element.clear(); element.sendKeys(value); String inputValue = element.getAttribute("value"); System.out.println(inputValue); } public void testClickLink() throws InterruptedException { WebElement element = driver.findElement(By.className("baidu")); System.out.println("The Link is: " + element.getAttribute("href")); element.click(); System.out.println("Current Page Title is:" + driver.getTitle()); Thread.sleep(3000); } public void testSelect(int index) { WebElement element = driver.findElement(By.name("select")); Select select = new Select(element); select.selectByIndex(index); String selectText = select.getFirstSelectedOption().getText(); System.out.println("Curretn Select is: " + selectText); } public void testRadioBox(int index) { Listelements = driver.findElements(By.name("identity")); WebElement element = elements.get(index); element.click(); boolean flag = element.isSelected(); getClass(); System.out.println("the radioBox is Select:" + flag); } public void testMultipleBox() { List elements = driver.findElements(By .xpath(".//*[@id='checkbox']/input")); for (WebElement element : elements) { element.click(); boolean flag = element.isSelected(); System.out.println("the CheckBox is select:" + flag); } } public void testBtn() { WebElement element = driver.findElement(By.className("button")); element.click(); String btnText = element.getAttribute("value"); System.out.println(btnText); } public void testAlert() throws InterruptedException { WebElement element = driver.findElement(By .xpath(".//*[@id='alert']/input")); element.click(); Thread.sleep(1000); Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); System.out.println("ALert Text is:" + alertText); alert.accept(); } // Selenium是无法识别Windows对话框的,所以在上传文件的过程中根据浏览器的不同,需要使用到AutoIT这个工具来获取Windows对话框实现上传的功能 public void testUploadFile(String filePath) { WebElement element = driver.findElement(By.id("load")); element.sendKeys(filePath); } public void testOpneNewWindow() { WebElement element = driver.findElement(By.xpath(".//*[@id='open']/a")); element.click(); String currentHandle = driver.getWindowHandle(); Set handles = driver.getWindowHandles(); Iterator iterators = handles.iterator(); while (iterators.hasNext()) { String handle = iterators.next(); if (!(currentHandle.equals(handle))) { driver.switchTo().window(handle); System.out.println("testOpneNewWindow Current Web Title: " + driver.getTitle()); this.closeBrowser(); } } driver.switchTo().window(currentHandle); } // 将鼠标移动到该Btn下面,就会有对应的信息显示 public void testAction() { WebElement element = driver.findElement(By.className("over")); Actions actions = new Actions(driver); actions.moveToElement(element).build().perform(); String actionText = driver.findElement(By.id("over")).getText(); System.out.println("The Action Text is: " + actionText); } //testAction弹出的信息得首先处理掉,不然testElementDisplayedWait无法定位到当前的页面,聚焦点依然在testAction上面的信息上面 public void testActionJavaScript() { JavascriptExecutor js2 = (JavascriptExecutor) driver; js2.executeScript("alert('Hello Selenium Web Test!')"); Alert alert = driver.switchTo().alert(); String text = alert.getText(); System.out.println(text); alert.accept(); } public void testElementDisplayedWait() { WebElement element = driver.findElement(By.className("wait")); element.click(); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(new ExpectedCondition () { @Override public Boolean apply(WebDriver driver2) { return driver2.findElement(By.className("red")).isDisplayed(); } }); System.out.println("The context is:" + driver.findElement(By.className("red")).getText()); } public static void main(String[] args) throws InterruptedException { BasicUIExercise bue = new BasicUIExercise(); bue.openBrowser(); bue.maxWindow(); bue.waitTime(); bue.goToTarget("E:\\JavaCodeWorkSpace\\JProject0610\\WebAddress\\demo2.html"); bue.testInput("Test WebDriver Study"); bue.testClickLink(); bue.backToTarget(); bue.testSelect(4); bue.testRadioBox(5); bue.testMultipleBox(); bue.testBtn(); bue.testAlert(); bue.executeJS("var q=document.documentElement.scrollTop=10000"); bue.testUploadFile("E:\\JavaCodeWorkSpace\\JProject0610\\WebAddress\\demo2.html"); bue.testOpneNewWindow(); bue.testAction(); bue.testActionJavaScript(); bue.testElementDisplayedWait(); Thread.sleep(1000); bue.quitBrowser(); }}