Selenium 元素定位

selenium通过driver.findElement(By selector)来定位元素,selector在selenium-java.jar中,里面的方法一共就8种,如下图:

下面提供了一些常见的方法:获取元素、判断元素是否存在、点击button、填写文本、等待事件

  1 import org.openqa.selenium.By;
  2 import org.openqa.selenium.NoSuchElementException;
  3 import org.openqa.selenium.WebDriver;
  4 import org.openqa.selenium.WebElement;
  5 import org.openqa.selenium.support.ui.Select;
  6
  7 /**
  8  * @auth  Nancy
  9  * @data  2016-11-23上午11:46:14
 10  * @description 页面元素工具
 11  */
 12 public class UIUtils {
 13
 14     public static int waitTimes = 5;
 15
 16
 17
 18 //***************************getBy****************************
 19
 20     /**
 21      * 判断页面元素是否存在
 22      * @param driver
 23      * @param selector
 24      * @return
 25      */
 26     public static boolean isElementExsit(WebDriver driver, By selector) {
 27         try {
 28             driver.findElement(selector);
 29             return true;
 30         } catch (NoSuchElementException e) {
 31             return false;
 32         }
 33     }
 34
 35     /**
 36      * 判断元素是否存在 通过ID
 37      * @param driver
 38      * @param id
 39      * @return
 40      */
 41     public static boolean isElementExsitById(WebDriver driver,String id) {
 42         try {
 43             driver.findElement(By.id(id));
 44             return true;
 45         } catch (NoSuchElementException e) {
 46             return false;
 47         }
 48     }
 49
 50     /**
 51      * 查找元素 通过By
 52      * @param driver
 53      * @param selector
 54      * @return
 55      */
 56     public static WebElement getElement(WebDriver driver, By selector){
 57         try {
 58             WebElement element = driver.findElement(selector);
 59             return element;
 60         } catch (NoSuchElementException e) {
 61             return null;
 62         }
 63     }
 64
 65     /**
 66      * 查找元素 通过tagName
 67      * @param driver
 68      * @param selector
 69      * @return
 70      */
 71     public static WebElement getElementTagName(WebDriver driver, String name){
 72         try {
 73             WebElement element = driver.findElement(By.tagName(name));
 74             return element;
 75         } catch (NoSuchElementException e) {
 76             return null;
 77         }
 78     }
 79
 80
 81     /**
 82      * 查找元素 通过Id
 83      * @param driver
 84      * @param id
 85      * @return
 86      */
 87     public static WebElement getElementById(WebDriver driver, String id){
 88         try {
 89             WebElement element = driver.findElement(By.id(id));
 90             return element;
 91         } catch (NoSuchElementException e) {
 92             new NoSuchElementException("找不到元素");
 93             return null;
 94         }
 95     }
 96     /**
 97      * 查找元素 通过xpath
 98      * @param driver
 99      * @param id
100      * @return
101      */
102     public static WebElement getElementByXpath(WebDriver driver, String xpath){
103         try {
104             WebElement element = driver.findElement(By.xpath(xpath));
105             return element;
106         } catch (NoSuchElementException e) {
107             new NoSuchElementException("找不到元素");
108             return null;
109         }
110     }
111
112     /**
113      *  查找元素 通过name
114      * @param driver
115      * @param name
116      * @return
117      */
118     public static WebElement getElementByName(WebDriver driver, String name){
119         try {
120             WebElement element = driver.findElement(By.name(name));
121             if(element!=null){
122                 return element;
123             }else{
124                 new NoSuchElementException("找不到元素");
125                 return null;
126             }
127         } catch (NoSuchElementException e) {
128             new NoSuchElementException("找不到元素");
129             return null;
130         }
131     }
132
133     /**
134      * 查找元素 通过超链接
135      * @param driver
136      * @param text
137      * @return
138      */
139     public static WebElement getElementByLinkText(WebDriver driver, String text){
140         try {
141             WebElement element = driver.findElement(By.linkText(text));
142             return element;
143         } catch (NoSuchElementException e) {
144             new NoSuchElementException(text+"元素找不到!");
145             return null;
146         }
147     }
148
149     /**
150      * 查找元素 通过className
151      * @param driver
152      * @param text
153      * @return
154      */
155     public static WebElement getElementByClassName(WebDriver driver, String className){
156         try {
157             WebElement element = driver.findElement(By.className(className));
158             return element;
159         } catch (NoSuchElementException e) {
160             new NoSuchElementException(className+"元素找不到!");
161             return null;
162         }
163     }
164
165
166 //***************************点击button****************************
167     /**
168      * 点击 button 超链接
169      * @param driver
170      * @param text
171      */
172     public static void clickByLinkText(WebDriver driver, String text){
173         WebElement element = getElementByLinkText(driver,text);
174         element.click();
175         wait(waitTimes);
176     }
177
178     /**
179      * 点击 button 通过Id
180      * @param driver
181      * @param id
182      */
183     public static void clickById(WebDriver driver,String id){
184         WebElement element = getElementById(driver, id);
185         element.click();
186         wait(waitTimes);
187     }
188     /**
189      * 点击 button 通过Id
190      * @param driver
191      * @param id
192      */
193     public static void clickByXpath(WebDriver driver,String xpath){
194         WebElement element = getElementByXpath(driver, xpath);
195         element.click();
196         wait(waitTimes);
197     }
198
199     /**
200      * 点击 button 通过name
201      * @param driver
202      * @param name
203      */
204     public static void clickByName(WebDriver driver,String name){
205         WebElement element = getElementByName(driver, name);
206         element.click();
207         wait(waitTimes);
208     }
209
210     /**
211      * 点击 button 通过classname
212      * @param driver
213      * @param className
214      */
215     public static void clickByClassName(WebDriver driver,String className){
216         WebElement element =getElementByClassName(driver, className);
217         element.click();
218         wait(waitTimes);
219     }
220
221 //***************************选择下拉框select****************************
222     public static Select selectBy(WebDriver driver,By by){
223         Select userSelect= new Select(getElement(driver, by));
224         return userSelect;
225     }
226
227     /**
228      * 获取明文为value的值
229      * @param driver
230      * @param by
231      * @param value
232      */
233     public static void selectVisualTextBy(WebDriver driver,By by,String visibleText){
234         Select userSelect= new Select(getElement(driver, by));
235         userSelect.selectByVisibleText(visibleText);
236     }
237
238     public static void selectVisualTextByName(WebDriver driver,String name,String visibleText){
239         selectVisualTextBy(driver, By.name(name), visibleText);
240     }
241
242 //***************************赋值input****************************
243     /**
244      * 通过id找到元素,赋值
245      * @param driver
246      * @param id id
247      * @param text 需要输入的文字
248      */
249     public static void sendKeysById(WebDriver driver, String id,String text){
250         WebElement element = getElementById(driver,id);
251         element.sendKeys(text);
252     }
253
254 //***************************等待****************************
255     /**
256      * 等待
257      * @param seconds
258      */
259     public static void waitMoment(){
260         wait(waitTimes);
261     }
262     /**
263      * 等待
264      * @param seconds
265      */
266     public static void wait(int seconds){
267         try {
268             Thread.sleep(seconds * 1000);
269         } catch (InterruptedException e) {
270             e.printStackTrace();
271         }
272     }
273
274     /**
275      * 等待元素最多三十秒
276      * @param driver
277      * @param id
278      * @return
279      */
280     public static boolean waitElementById(WebDriver driver,String id){
281         boolean flag = false;
282         int i = 1;
283         while(!flag&i<=6){
284             wait(waitTimes);
285             flag = isElementExsitById(driver,id);
286             i++;
287         }
288         return flag;
289     }
290
291     /**
292      * 通过超链接等待元素
293      * @param driver
294      * @param text
295      * @return
296      */
297     public static boolean waitElementByLinkText(WebDriver driver,String text){
298         boolean flag = false;
299         int i = 1;
300         while(!flag&i<=6){
301             wait(waitTimes);
302             flag = isElementExsit(driver,By.linkText(text));
303             i++;
304         }
305         return flag;
306     }
307     
时间: 2024-08-01 15:12:27

Selenium 元素定位的相关文章

Selenium元素定位学习教程

无论哪一种自动化测试的驱动框架(基于B/S,桌面应用,还是手机App).都应当具有一套优秀的元素定位技术.通常的自动化测试流程也可以简单的归结为是一个从被测试程序中识别或是定位元素以及执行操作和验证元素的过程.这一篇我们就一起学习Selenium以及它是如何定位DOM元素的.本文将会介绍如下内容: Selenium DOM 主要的定位方式. Selenium 如何扩展元素定位方式. 辅助浏览器工具 (一)Selenium DOM主要定位方式 上一篇中,我们介绍了WebDriver和 WebEle

selenium元素定位不到的解决方式

1.如果确信自己的元素的地址写的事正常的,但是就是没有反应的话可以试试切换Frame下 代码为: driver.switch_to.frame("mainFrame") 用完之后记得切换回原来的 driver.switch_to.default_content() 2.对xpath 和css元素定位不是很清楚的话,可以使用的firefox的工具,selenium IDE,通过脚本录制在导出,查看脚本文件,可以很快定位到元素的位置 3.有些元素在打开文件之后,需要一定的等待时间,可以加上

selenium元素定位大全

要做自动化,首先要了解页面结构,要了解页面结构,就要了解页面元素的定位方法 在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素. 常用的元素定位方法如下: 如何选择 定位方法 策略是:选择简单,稳定的定位方法. 1. 当页面元素有id属性的时候, 尽量使用id来定位. 没有的话,再选择其他定位方法 2. cssSelector 执行速度快, 推荐使用 3. 定位超链接的时候,可以考虑linkTe

python selenium 元素定位(三)

上两篇的博文中介绍了python selenium的环境搭建和编写的第一个自动化测试脚本,从第二篇的例子中看出来再做UI级别的自动化测试的时候,有一个至关重要的因素,那就是元素的定位,只有从页面上找到这个元素,我们从能对这个元素进行操作,那么我们下来看看如何来定位元素. selenium 提供了8中元素定位的方法(大家要学习元素的定位,首先可以学习下前端的基础知识,这样有利于我们学习自动化测试,大家可以看一下:http://www.runoob.com/) find_element_by_id

selenium元素定位

1.元素定位 在本章中,我们将讨论 u 使用浏览器工具来检查页面中元素的结构 u 使用findElement方法定位元素 u 使用findElements方法来定位元素 u 定位链接 u 通过标签名称定位元素 u 使用CSS选择器定位元素 u 使用XPath定位元素 u 使用文本定位元素 u 使用高级CSS选择器定位元素 u 使用jQuery选择器 u 定位表格的行和列 u 定位表格中的子元素 1.1.介绍 成功的自动化GUI(图形用户界面)测试取决于从被测试的应用程序中识别和定位GUI元素,然

selenium元素定位不到之iframe

我们在使用selenium的18中定位方式的时候,有时会遇到定位不上的问题,今天我们就来说说导致定位不上的其中一个原因---iframe 问题描述:通过firebug查询到相应元素的id或name等,但在定位此元素的时候就是报错,提示: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element 问题原因:元素定位不到 问题分析:通过firebug可以看到,但就是定位不到,这时候很有可能

selenium元素定位(三)

使用selenium就不可避免的要提到界面元素定位,通过元素定位来实现一系列的逻辑操作. selenium提供了8中元素定位的方式: id.name.class name.tag name.link text.partial link text.xpath.css selector. 其中使用xpath来根据id或class来定位元素的可以解决大部分问题. 这8中定位方式在python selenium中使用的对应具体方法为: 1 find_element_by_id() 2 find_elem

Java + selenium 元素定位(5)之By Xpath

这篇关于Xpath方法的文章和之前那篇CSS的方法一样,使用前,需要先掌握一些Xpath的相关知识.当然,网上也有各种工具可以帮助我们获取到元素的Xpath,但是这并不代表着我们就可以不用了解Xpath的相关知识,毕竟依赖工具永远没有依赖自己来着靠谱.但是我也会介绍一下如果简单快速的获取元素的Xpath. 我尝试过使用一些文章介绍的快速获取Xpath的工具,但是可能因为这些工具都是依赖于浏览器的扩展工具,更新换代比较快,所以文章介绍的工具我并没有成功找到.我在这里要介绍的方法,不需要使用额外的工

selenium元素定位方法介绍

元素定位方法 元素名称 webdriver API id find_element_by_id() name find_element_by_name() class name find_element_by_class_name() tag name find_element_by_tag_name() link text find_element_by_link_text() partial link text find_element_by_partial_link_text() xpat

java+selenium元素定位和元素操作

1.元素定位 ID定位元素: findElement(By.id("")); 通过元素的名称定位元素: findElement(By.name("")); 通过元素的html中的位置定位元素: findElement(By.xpath("")); 通过元素的标签名称定位元素: findElement(By.tagName("")); 通过元素的链接名称定位元素: findElement(By.linkText("&q