selenium之ExpectedConditions類
??以前使用eclipse感覺不是很習慣,現在使用intelliJ IDEA編寫代碼。下面的是ExpectedConditions
類下的一些方法,使用IDEA時會有方法提示。
??下面的是我自己根據實際工程中遇到的問題寫的一些方法
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public IOSElement findElementByName(String name) {
IOSElement button = (IOSElement) new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfElementLocated(By.name(name)));
return button;
}
??這段代碼實現的功能是設立一個顯式等待時間30秒,如果在30秒內找到這個元素,就返回IOSElement
類型的元素。然后接下來就可以對這個元素進行操作。
public List<IOSElement> findElementsByClassName(String className){
List<IOSElement> elements = (List)new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.className(className)));
return elements;
??這段代碼返回同一個界面上的所有的具有相同classname
的元素。
public IOSElement findElementByClassName(String ClassName,int i){
List<IOSElement> Element = (List)new WebDriverWait(driver, 30) .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.className(ClassName)));
return Element.get(i);
}
??這段代碼以上段代碼為基礎,比如要取到具有相同classname
列表中所有元素中的第i個元素,就可以使用List
的get
方法,get(i)
取到列表中索引值為i
的對象。