selenium獲取頁面元素方法歸納總結
阿新 • • 發佈:2019-02-17
每次用到selenium元素時,都動態獲取,每次重複的動作,很是麻煩,寫了一個簡單的函式將不同的獲取方法總結一下,每次跳轉到新頁面時,統一調取函式獲取所涉及的元素即可
public WebElement get_webElement(WebDriver d, char optiontype, String value)throws NoSuchElementException{
WebElement element = null;
if (d == null || optiontype == ’ ’ || value == null){
System.out.println(“Function[get_webElement] error:Parameters must not NULL”);
return null;
}
switch(optiontype){ case 'C': element = d.findElement(By.className(value)); return element; case 'i': element = d.findElement(By.id(value)); System.out.println("finish"); return element; case 'n': element = d.findElement(By.name(value)); return element; case 'l': element = d.findElement(By.linkText(value)); return element; case 't': element = d.findElement(By.tagName(value)); return element; case 'c': element = d.findElement(By.cssSelector(value)); return element; case 'p': element = d.findElement(By.partialLinkText(value)); return element; case 'x': element = d.findElement(By.xpath(value)); return element; default: return element; }
}
其中,WebDriver 代表瀏覽器驅動;optiontype代表按什麼型別獲取元素;value表示元素對應型別的值