博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Selenium的自我总结2_元素基本操作
阅读量:5359 次
发布时间:2019-06-15

本文共 7202 字,大约阅读时间需要 24 分钟。

          对于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) {        List
elements = 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(); }}

转载于:https://www.cnblogs.com/taoSir/p/4826656.html

你可能感兴趣的文章
Saiku资源帖
查看>>
解决手机页面中点击文本框,网页放大问题
查看>>
2-5
查看>>
牛客多校3 A-PACM Team(状压降维+路径背包)
查看>>
HDU - 4284 Travel(floyd+状压dp)
查看>>
1027 制作表格
查看>>
Android之Socket通信、List加载更多、Spinner下拉列表
查看>>
面向对象的介绍与特性
查看>>
typing-python用于类型注解的库
查看>>
20189215 2018-2019-2 《密码与安全新技术专题》第13周作业
查看>>
第四周作业
查看>>
一、HTML基础
查看>>
蓝牙进阶之路 (002) - HC-05与HC-06的AT指令的区别(转)
查看>>
mysql的limit经典用法及优化
查看>>
C#后台程序与HTML页面中JS方法互调
查看>>
mysql 同一个表中 字段a 的值赋值到字段b
查看>>
linux系统可执行文件添加环境变量使其跨终端和目录执行
查看>>
Window7通过Anaconda安装Tensorflow
查看>>
antiSMASH数据库:微生物次生代谢物合成基因组簇查询和预测
查看>>
UNICODE与ANSI的区别
查看>>