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
的对象。