1、对于selenium_java还是封装一下element的定位方法吧,否则,每次查找元素后都非空判断一下好麻烦。
import java.util.ArrayList; 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.interactions.Actions; import org.testng.Assert; import org.testng.Reporter; import org.testng.log4testng.Logger; import cn.com.cnpc.richfit.cloud.resourceMan.testcase.CloudTestBase; import cn.com.cnpc.richfit.cloud.resourceMan.utils.CommonProperties; import cn.com.cnpc.richfit.cloud.resourceMan.utils.TimeUtils; /** * 查找元素的工具类 * @author lenovo * */ public class CloudElementMethods { public WebElement element = null; public List<WebElement> elementList = new ArrayList<WebElement>(); private WebDriver driver = null; private static final Logger LOG = Logger.getLogger(CloudElementMethods.class); private static final int DELAY_NUM = 2;// 默认延迟次数 private static final long DELAY_STEP_LENGTH = 500;// 默认延迟步长,500毫秒 public void setDriver(WebDriver driver) { this.driver = driver; } /** * id获取单个元素 * * @param id * @return */ public WebElement getElementById(String id) { LOG.info("getElementById method called"); Reporter.log("getElementById method called"); System.out.println("getElementById method called"); try { element = driver.findElement(By.id(id)); if (element == null) { for (int i = 0; i < DELAY_NUM; i++) { TimeUtils.waitfor(DELAY_STEP_LENGTH); element = driver.findElement(By.id(id)); if (element != null) { break; } } } } catch (Exception e) { LOG.info("Can not find element:" + id); Reporter.log("Can not find element:" + id); System.out.println("Can not find element:" + id); element = null; // Assert.fail("Can not find element:"+id); return element; } return element; } /** * id获取单个元素-从父级元素获取 * * @param id * @return */ public WebElement getElementById(WebElement fatherElement, String id) { LOG.info("getElementById method called"); Reporter.log("getElementById method called"); System.out.println("getElementById method called"); try { element = fatherElement.findElement(By.id(id)); if (element == null) { for (int i = 0; i < DELAY_NUM; i++) { TimeUtils.waitfor(DELAY_STEP_LENGTH); element = fatherElement.findElement(By.id(id)); if (element != null) { break; } } } } catch (Exception e) { LOG.info("Can not find element:" + id); Reporter.log("Can not find element:" + id); System.out.println("Can not find element:" + id); element = null; // Assert.fail("Can not find element:"+id); return element; } return element; } /** * classname获取单个元素 * * @param classname * @return */ public WebElement getElementByClassName(String classname) { LOG.info("getElementByClassName method called"); Reporter.log("getElementByClassName method called"); System.out.println("getElementByClassName method called"); try { element = driver.findElement(By.className(classname)); if (element == null) { for (int i = 0; i < DELAY_NUM; i++) { TimeUtils.waitfor(DELAY_STEP_LENGTH); element = driver.findElement(By.className(classname)); if (element != null) { break; } } } } catch (Exception e) { // TODO: handle exception LOG.info("Can not find element:" + classname); Reporter.log("Can not find element:" + classname); System.out.println("Can not find element:" + classname); element = null; // Assert.fail("Can not find element:"+classname); return element; } return element; } /** * classname获取单个元素-从父级元素获取 * * @param classname * @return */ public WebElement getElementByClassName(WebElement fatherELement, String classname) { if (fatherELement!=null) { LOG.info("getElementByClassName method called"); Reporter.log("getElementByClassName method called"); System.out.println("getElementByClassName method called"); try { element = fatherELement.findElement(By.className(classname)); if (element == null) { for (int i = 0; i < DELAY_NUM; i++) { TimeUtils.waitfor(DELAY_STEP_LENGTH); element = fatherELement.findElement(By.className(classname)); if (element != null) { break; } } } } catch (Exception e) { // TODO: handle exception LOG.info("Can not find element:" + classname); Reporter.log("Can not find element:" + classname); System.out.println("Can not find element:" + classname); element = null; // Assert.fail("Can not find element:"+classname); return element; } } return element; } /** * classname获取多个元素 * * @param classname * @return */ public List<WebElement> getElementsByClassName(String classname) { LOG.info("getElementsByClassName method called"); Reporter.log("getElementsByClassName method called"); System.out.println("getElementsByClassName method called"); try { elementList = driver.findElements(By.className(classname)); if (elementList == null) { for (int i = 0; i < DELAY_NUM; i++) { TimeUtils.waitfor(DELAY_STEP_LENGTH); elementList = driver.findElements(By.className(classname)); if (elementList != null) { break; } } } } catch (Exception e) { // TODO: handle exception LOG.info("Can not find elements:" + classname); Reporter.log("Can not find element:" + classname); System.out.println("Can not find element:" + classname); elementList = null; // Assert.fail("Can not find element:"+classname); return elementList; } return elementList; } /** * classname获取多个元素-从父级元素获取 * * @param classname * @return */ public List<WebElement> getElementsByClassName(WebElement fatherElement, String classname) { if (fatherElement!=null) { LOG.info("getElementsByClassName method called"); Reporter.log("getElementsByClassName method called"); System.out.println("getElementsByClassName method called"); try { elementList = fatherElement.findElements(By.className(classname)); if (elementList == null) { for (int i = 0; i < DELAY_NUM; i++) { TimeUtils.waitfor(DELAY_STEP_LENGTH); elementList = fatherElement.findElements(By.className(classname));