首页 > 代码库 > Selenium 元素定位

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     

 

Selenium 元素定位