一、如何找到页面元素

Webdriver的findElement方法可以用来找到页面的某个元素,最常用的方法是用id和name查找。下面介绍几种比较常用的方法。

1.1By ID

假设页面写成这样:input type="text" name="passwd"id="passwd-id"

那么可以这样找到页面的元素:

通过id查找:

WebElement element = driver.findElement(By.id("passwd-id"));

1.2 By Name

或通过name查找:

WebElement element = driver.findElement(By.name("passwd"));

1.3 By XPATH(右键--审查元素

或通过xpath查找:

WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));

1.4 By Class Name

假设页面写成这样:

Cheddar

Gouda

可以通过这样查找页面元素:

Listcheeses = driver.findElements(By.className("cheese"));

1.5 By Link Text

假设页面元素写成这样:

cheese>

那么可以通过这样查找:

WebElement cheese =driver.findElement(By.linkText("cheese"));

二、如何对页面元素进行操作

找到页面元素后,怎样对页面进行操作呢?我们可以根据不同的类型的元素来进行一一说明。

2.1 输入框(text field or textarea)

找到输入框元素:

WebElement element = driver.findElement(By.id("passwd-id"));

在输入框中输入内容:

element.sendKeys(“test”);

将输入框清空:

element.clear();

获取输入框的文本内容:

element.getText();

2.2 下拉选择框(Select)

找到下拉选择框的元素:

Select select = new Select(driver.findElement(By.id("select")));

选择对应的选择项:

select.selectByVisibleText(“mediaAgencyA”);

select.selectByValue(“MA_ID_001”);

不选择对应的选择项:

select.deselectAll();

select.deselectByValue(“MA_ID_001”);

select.deselectByVisibleText(“mediaAgencyA”);

或者获取选择项的值:

select.getAllSelectedOptions();

select.getFirstSelectedOption();

2.3 单选项(Radio Button)

找到单选框元素:

WebElement bookMode =driver.findElement(By.id("BookMode"));

选择某个单选项:

bookMode.click();

清空某个单选项:

bookMode.clear();

判断某个单选项是否已经被选择:

bookMode.isSelected();

2.4 多选项(checkbox)

多选项的操作和单选的差不多:

WebElement checkbox =driver.findElement(By.id("myCheckbox."));

checkbox.click();

checkbox.clear();

checkbox.isSelected();

checkbox.isEnabled();

2.5 按钮(button)

找到按钮元素:

WebElement saveButton = driver.findElement(By.id("save"));

点击按钮:

saveButton.click();

判断按钮是否enable:

saveButton.isEnabled ();

2.6 左右选择框

也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。例如:

Select lang = new Select(driver.findElement(By.id("languages")));

lang.selectByVisibleText(“English”);

WebElement addLanguage =driver.findElement(By.id("adon"));

addLanguage.click();

2.7 弹出对话框(Popup dialogs)

Alert alert = driver.switchTo().alert();

alert.accept();

alert.dismiss();

alert.getText();

2.8 表单(Form)

Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:

WebElement approve = driver.findElement(By.id("approve"));

approve.click();

approve.submit();//只适合于表单的提交

2.9 上传文件 (Upload File)

上传文件的元素操作:

WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));

String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";

adFileUpload.sendKeys(filePath);

2.10 Windows 和 Frames之间的切换

一般来说,登录后建议是先:

driver.switchTo().defaultContent();

切换到某个frame:

driver.switchTo().frame("leftFrame");

从一个frame切换到另一个frame:

driver.switchTo().frame("mainFrame");

切换到某个window:

driver.switchTo().window("windowName");

2.11拖拉(Drag andDrop)

WebElement element =driver.findElement(By.name("source"));

WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();

2.12导航 (Navigationand History)

打开一个新的页面:

driver.navigate().to("http://www.example.com");

通过历史导航返回原页面:

driver.navigate().forward();

driver.navigate().back();

三、RemoteWebDriver

当本机上没有浏览器,需要远程调用浏览器进行自动化测试时,需要用到RemoteWebDirver.

3.1 使用RemoteWebDriver

import java.io.File;

import java.net.URL;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.remote.Augmenter;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

public class Testing {

public void myTest()throws Exception {

WebDriver driver = newRemoteWebDriver(

new URL("http://localhost:4446/wd/hub"),

DesiredCapabilities.firefox());

driver.get("http://www.google.com");

// RemoteWebDriverdoes not implement the TakesScreenshot class

// if the driver doeshave the Capabilities to take a screenshot

// then Augmenter willadd the TakesScreenshot methods to the instance

WebDriveraugmentedDriver = new Augmenter().augment(driver);

File screenshot =((TakesScreenshot)augmentedDriver).

getScreenshotAs(OutputType.FILE);

}

}

3.2 SeleniumServer

在使用RemoteDriver时,必须在远程服务器启动一个SeleniumServer:

java -jar selenium-server-standalone-2.20.0.jar -port 4446

3.3 How to setFirefox profile using RemoteWebDriver

profile = new FirefoxProfile();

profile.setPreference("general.useragent.override",testData.getUserAgent());

capabilities = DesiredCapabilities.firefox();

capabilities.setCapability("firefox_profile", profile);

driver = new RemoteWebDriver(new URL(“http://localhost:4446/wd/hub”),capabilities);

driverWait = new WebDriverWait(driver,TestConstant.WAIT_ELEMENT_TO_LOAD);

driver.get("http://www.google.com");

selenium元素定位方法的更多相关文章

  1. python之selenium元素定位方法

    前提: 大家好,今天我们来学习一下selenium,今天主要讲解selenium定位元素的方法,希望对大家有所帮助! 内容: 一,selenium定位元素 selenium提供了8种方法: 1.id ...

  2. selenium元素定位方法之轴定位

    一.轴运算名称 ancestor:祖先结点(包括父结点) parent:父结点 preceding:当前元素节点标签之前的所有结点(html页面先后顺序) preceding-sibling:当前元素 ...

  3. selenium自动化测试——常见的八种元素定位方法

    selenium常用的八种元素定位方法 1.通过 id 定位:find_element_by_id() 2.通过 name 定位:find_element_by_name() 3.通过 tag 定位: ...

  4. 不支持find_element_by_name元素定位方法,抛不支持find_element_by_name元素定位方法,会抛如下错误 org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session的解决

    appium1.5后不支持find_element_by_name元素定位方法,会抛如下错误 org.openqa.selenium.InvalidSelectorException: Locator ...

  5. Selenium之WebDriver元素定位方法

    Selenium WebDriver 只是 Python 的一个第三方框架, 和 Djangoweb 开发框架属于一个性质. webdriver 提供了八种元素定位方法,python语言中也有对应的方 ...

  6. [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)

    转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...

  7. python+selenium元素定位——8种方法

    定位元素,selenium提供了8中元素定位方法: (1)find_element_by_id() :html规定,id在html中必须是唯一的,有点类似于身份证号 (2)find_element_b ...

  8. selenium的定位方法-多元素定位

    在实际工作中,有些时候定位元素使用ID.NAME.CLASS_NMAE.XPATH等方法无法定位到具体元素,会发现元素属性有很多一致的,这个时候使用单元素定位方法无法准确定位到具体元素,例如,百度首页 ...

  9. selenium的定位方法-单元素定位

    selenium自动化测试中,提供了单个元素定位方法,多个元素定位方法,2种方式都是根据元素属性:ID.NAME.CLASS_NAME.TAG_NAME.CSS_SELECTOR.XPATH.LINK ...

随机推荐

  1. (转)Memcached 之 .NET(C#)实例分析

    一:Memcached的安装 step1. 下载memcache(http://jehiah.cz/projects/memcached-win32)的windows稳定版(这里我下载了memcach ...

  2. Java Service Wrapper简介与使用(转)

    本文转自https://www.cnblogs.com/zcy_soft/p/3738947.html,写的非常好,珍藏一下 1. wrapper的意思? wrapper在此处理解为“包装”. 2. ...

  3. redis之 集群配置(主从复制)

    集群的作用: 集群的方式有二种: 第二种的好处是:master宕机以后可以直接切换到slave1 主从通信的过程 先通过dump导出大块的rdb 然后把aof缓冲过来,然后通过replicationf ...

  4. Swagger与SpringMVC项目整合

    Swagger与SpringMVC项目整合 来源:http://www.2cto.com/kf/201502/376959.html 为了方便的管理项目中API接口,在网上找了好多关于API接口管理的 ...

  5. Tomcat的配置文件Server.xml解析

    配置元素说明: 元素名 属性 解释 server port 指定一个端口,这个端口负责监听关闭tomcat 的请求 shutdown 指定向端口发送的命令字符串 service name 指定serv ...

  6. EF4.4增删改查实例

    第一.先创建一个名为Store数据库,将下面脚本代码执行创建表: USE [Store] GO /****** Object: Table [dbo].[Category] Script Date: ...

  7. react-native-image-picker在IOS上总是返回”Can’t find variable:response”的错误?

    环境: react-native: 0.41.2 react-native-image-picker: 0.26.2 xcode 8.2.1 iphone 6 根据官方教程(https://githu ...

  8. C# 核心语法-反射(反射类型、方法、构造函数、属性,实现可配置可扩展,完成数据库访问类反射封装)

    反射是.NET中的重要机制,通过反射,可以在运行时获得程序或程序集中每一个类型(包括类.结构.委托.接口和枚举等)的成员和成员的信息.有了反射,即可对每一个类型了如指掌.另外我还可以直接创建对象,即使 ...

  9. jquery datatables 学习笔记

    最近项目中用到了BootStrap做后台,在选择表格插件的时候发现了jquery datatables. 功能是很强大,但是网上的例子比较少.在经过一段时间的努力可算是搞出来了. 官网地址:http: ...

  10. 怎么让一段xml被识别为字符串

    public static String decodeString(String strData) { strData = replaceString(strData, "<" ...