Selenium2(WebDriver)总结(四)---基本元素操作

简介: WebDriver提供了常用的WEB控件的操作方法,比如:按钮、输入框、超链接等,废话不多说,直接上代码: import org.openqa.selenium.By; import org.

 WebDriver提供了常用的WEB控件的操作方法,比如:按钮、输入框、超链接等,废话不多说,直接上代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class WebDriverSimple {
    
    WebDriver driver;    
    
    public WebDriverSimple()
    {
     System.setProperty(
"webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe"); this.driver = new FirefoxDriver(); } //启动浏览器 public void startBrowser(String url) { driver.get(url); //最大化浏览器 driver.manage().window().maximize(); } //使用xpath获取元素对象 public WebElement locateElementByXpath(String locator) { WebElement element = driver.findElement(By.xpath(locator)); return element; } //使用CSS获取元素对象 public WebElement locateElementByCss(String locator) { WebElement element = driver.findElement(By.cssSelector(locator)); return element; } /**操作输入框input * 1.sendKeys代表输入,参数为要输入的值 * 2.clear代表清除输入框中原有的数据 */ public void testInput(String locator,String content) { //WebElement input = this.locateElementByXpath(locator); WebElement input = this.locateElementByCss(locator); input.clear(); input.sendKeys(content); input.clear(); input.sendKeys(content); } // /**操作超链接link * 1.click代表点击这个a链接 */ public void testLink(String locator) { WebElement link = this.locateElementByXpath(locator); link.click(); } /**操作 下拉框 select * 1.需要一个Select的类 * 2.selectByValue的参数为option中的value属性 * 3.selectByIndex的参数为option的顺序 * 4.selectByVisibleText的参数为option的text值 */ public void testSelect(String locator,int index) { WebElement element = this.locateElementByXpath(locator); Select select = new Select(element); select.selectByIndex(index); } public void testSelect(String locator,String value) { WebElement element = this.locateElementByXpath(locator); Select select = new Select(element); select.selectByValue(value); //select.selectByVisibleText(text); } /**操作单选按钮radiobox * 1.click代表点击选中这个单选框 * 2.isSelected代表检查这个单选框有没有被选中 */ public void testRaidoBox(String locator) { WebElement radio = this.locateElementByXpath(locator); radio.click(); radio.isSelected(); } /**操作 复选框checkbox * 1.click代表点击选中这个多选框 * 2.isSelected代表检查这个多选框有没有被选中 */ public void testCheckBox(String locator) { WebElement checkbox = this.locateElementByXpath(locator); checkbox.click(); checkbox.isSelected(); } /**操作 按钮button * 1.click代表点击这个按钮 * 2.sEnabled代表检查这个按钮是不是可用的 */ public void testButton(String locator) { WebElement submit = this.locateElementByXpath(locator); submit.click(); submit.isEnabled(); }

    /**操作 上传控件upload
    * 1.一般是把路他径直接sendKeys到这个输入框中
    * 2.如果输入框被加了readonly属性,不能输入,则需要用JS来去掉readonly属性!
    */

      public void testUpload(String locator,String path)

    {
        WebElement load = this.locateElementByXpath(locator);
        load.sendKeys(path);
    }
    
    //关闭并退出driver
    public void closeBrowser()
    {
        driver.close();
        driver.quit();
    }
    
    
}

 

目录
相关文章
|
8月前
|
Web App开发 JavaScript
2021最新Selenium真正绕过webdriver检测
2021最新Selenium真正绕过webdriver检测
192 0
|
1天前
|
Java 测试技术 持续交付
深入理解与应用Selenium WebDriver进行自动化测试
【4月更文挑战第25天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加速市场发布的关键步骤。Selenium WebDriver作为业界广泛采用的自动化测试工具之一,提供了一种灵活且高效的方式来模拟用户与Web应用程序交互。本文将探讨Selenium WebDriver的核心概念、架构以及实际应用中的技巧和最佳实践。通过深入分析其工作原理及常见问题解决方案,旨在帮助测试工程师提升测试效率,确保测试结果的准确性和可靠性。
|
9天前
|
Java 测试技术 定位技术
《手把手教你》系列技巧篇(二十三)-java+ selenium自动化测试-webdriver处理浏览器多窗口切换下卷(详细教程)
【4月更文挑战第15天】本文介绍了如何使用Selenium进行浏览器窗口切换以操作不同页面元素。首先,获取浏览器窗口句柄有两种方法:获取所有窗口句柄的集合和获取当前窗口句柄。然后,通过`switchTo().window()`方法切换到目标窗口句柄。在项目实战部分,给出了一个示例,展示了在百度首页、新闻页面和地图页面之间切换并输入文字的操作。最后,文章还探讨了在某些情况下可能出现的问题,并提供了一个简单的本地HTML页面示例来演示窗口切换的正确操作。
38 0
|
9月前
|
Web App开发 数据采集 JavaScript
Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
Youtube 是一个非常流行的视频分享平台,有时候我们可能想要爬取一些视频的信息,比如标题、播放量、点赞数等。但是有些信息并不是直接显示在网页上的,而是需要我们将鼠标悬停在某个元素上才能看到,比如视频的时长、上传时间等。这些信息被称为悬停文本,它们是通过 JavaScript 动态生成的,所以我们不能用普通的 HTML 解析方法来获取它们。那么,我们该如何用爬虫来获取 Youtube 的悬停文本呢?本文将介绍一种方法,使用 Selenium Chrome Webdriver 来模拟浏览器操作,获取 Youtube 的悬停文本。
118 0
Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
|
Web App开发 JavaScript 测试技术
Selenium Webdriver 简易教程2
Selenium Webdriver 简易教程2
93 0
|
Web App开发 前端开发 JavaScript
Selenium Webdriver 简易教程
Selenium Webdriver 简易教程
95 0
|
JavaScript
selenium webdriver执行远程 第三方js解决方案
selenium webdriver执行远程 第三方js解决方案
selenium webdriver执行远程 第三方js解决方案
|
API 索引
selenium源码通读·13 |webdriver/support分析
selenium源码通读·13 |webdriver/support分析
88 0
selenium源码通读·13 |webdriver/support分析
|
移动开发 JavaScript 前端开发
selenium源码通读·12 |webdriver/remote分析
selenium源码通读·12 |webdriver/remote分析
155 0
selenium源码通读·12 |webdriver/remote分析
|
存储 API
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析
68 0
selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析

热门文章

最新文章