Java Selenium:遍历元素列表
我正在使用XPath/CSS和Selenium来定位网站上的元素。我想创建一个方法,在这个方法中,我遍历一个定位器列表(XPath/CSS),然后程序选择哪个有效。换句话说,它从定位器1开始-如果定位器存在,它将返回true并存在循环。否则,它将移动到列表中的下一个定位器。一旦用完所有CSS定位器,它就会转到XPath等等 目前,我正在考虑如下实施:Java Selenium:遍历元素列表,java,css,selenium,xpath,Java,Css,Selenium,Xpath,我正在使用XPath/CSS和Selenium来定位网站上的元素。我想创建一个方法,在这个方法中,我遍历一个定位器列表(XPath/CSS),然后程序选择哪个有效。换句话说,它从定位器1开始-如果定位器存在,它将返回true并存在循环。否则,它将移动到列表中的下一个定位器。一旦用完所有CSS定位器,它就会转到XPath等等 目前,我正在考虑如下实施: public boolean iterate(WebDriver driver, By selectorType, String[] locato
public boolean iterate(WebDriver driver, By selectorType, String[] locator)
{
driver.get("URL");
for(int selectorListCounter = 0; selectorListCounter < locator.length; selectorListCounter++) {
try
{
driver.findElement(By.(selectorType)).sendText();
System.out.println("CSS Selector: " + CSS + " found");
return true;
} catch (Exception e)
{
System.out.println(CSS + " CSS Selector Not Present");
return false;
}
}
public boolean iterate(WebDriver驱动程序,按selectorType,字符串[]定位器)
{
获取驱动程序(“URL”);
对于(int-selectorListCounter=0;selectorListCounter
然后,我计划为每种定位器类型调用此方法(一次用于XPath,一次用于CSS等)
这是最好的方法吗?我认为下面的解决方案适合您,我认为没有必要为每种类型的定位器循环一次。
findElement()
不关心您使用什么机制来定位元素,只要它是有效的机制
//Define as many locators as you like
private static final By cssLaunchIcon() {return By.css("div.myDemoItemActions i.launchIcon");}
private static final By cssLaunchIcon1() {return By.css("div.myDemoItemActions i.launchIcon.a");}
private static final By cssLaunchIcon2() {return By.css("div.myDemoItemActions i.launchIcon.li");}
private static final By xpathLauncIcon() {return By.xpath("//ul/li/a[text()='launch']");}
private static final By idLaunchIcon() {return By.id("launchIcon");}
//Initialise the non empty list of locators
List<By> locators= Arrays.asList(cssLaunchIcon(), xpathLauncIcon(), idLaunchIcon(), cssLaunchIcon1(), cssLaunchIcon2());
public booolean findElement(List<By> locators) {
Iterator<By> locs = locators().iterator();
boolean found = false;
//while iterator has another locator && no locator found yet
while(locs.hasNext && !found) {
WebElement el = locs.next();
try{
if(driver.findElement(el).isDisplayed) {
found = true
}
} catch(NoSuchElementException e) {
System.out.println("Could not find " + el)
}
return found;
}
//定义任意多个定位器
私有静态final By cssLaunchIcon(){return By.css(“div.myDemoItemActions i.launchIcon”);}
私有静态final By cssLaunchIcon1(){return By.css(“div.myDemoItemActions i.launchIcon.a”);}
私有静态final By cssLaunchIcon2(){return By.css(“div.myDemoItemActions i.launchIcon.li”);}
xpathLauncIcon()的私有静态final({return By.xpath(“//ul/li/a[text()='launch']”;}
idLaunchIcon()的私有静态final{return By.id(“launchIcon”);}
//初始化定位器的非空列表
列表定位器=Arrays.asList(cssLaunchIcon(),xpathLauncIcon(),idLaunchIcon(),cssLaunchIcon1(),cssLaunchIcon2());
public booolean findElement(列表定位器){
迭代器locs=定位器()。迭代器();
布尔值=false;
//而迭代器有另一个定位器&&尚未找到定位器
while(locs.hasNext&&!找到){
WebElement el=locs.next();
试一试{
if(显示驱动程序findElement(el){
找到=真
}
}捕获(无接触元素例外e){
System.out.println(“找不到”+el)
}
发现退货;
}
事实上,我昨天这样做只是为了加快我的结果处理过程。
而不是成功和失败,你有选择1和选择2
隐式等待时间是该程序中每个元素检查1秒。因此while循环总共持续8秒(因为它每个元素生成两个数组)。此操作通过将元素放入数组来检查元素是否存在,然后检查数组的大小。如果元素不存在,则数组将为空。但是,如果其中一个条件失败,则表示页面上存在一个元素
然后在下面设置布尔值,找出页面上出现了哪些元素,并捕获不存在的元素的错误
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
int checkForElement=1;
success = false; failure = false;
while (driver.findElements(By.className("successMessage")).size()==0 && driver.findElements(By.className("errormessage")).size()==0 && checkForElement<=4 )
{
checkForElement+=1;
}
checkForElement=1;//reset variable
try
{
@SuppressWarnings("unused")//suppresses the warning so that the class is clean
WebElement successful = driver.findElement(By.className("successMessage"));//not used practically, but logically used to look for the presence of an element without waiting the normal implicit wait time I would have at 6 seconds
success = true;
}
catch(Exception e)
{
success = false;
}
try
{
@SuppressWarnings("unused")//suppresses the warning so that the class is clean
WebElement failing = driver.findElement(By.className("errormessage"));//not used practically, but logically used to look for the presence of an element without waiting the normal implicit wait time I would have at 6 seconds
failure = true;
}
catch(Exception e)
{
failure = false;
}
if(success)
{
//run success code
}
else if(failure)
{
//run failure code
}
driver.manage().timeouts().implicitlyWait(1,TimeUnit.SECONDS);
int checkForElement=1;
成功=错误;失败=错误;
while(driver.findElements(By.className(“successMessage”)).size()=0&&driver.findElements(By.className(“errormessage”)).size()==0&&CHECKFORELEMENT您能告诉我,为什么需要这个吗?其中一个答案有用吗?或者您决定使用另一个解决方案吗?我发现此代码有两个问题..1.一旦找不到元素,代码就会崩溃(NoSuchElementException)。我正在使用try/catch来解决这个问题。2.传递多个CSS如何(或多个XPath)选择器?代码仅适用于1 CSS/1 XPath..1.我忽略了。2.您可以添加任意多个定位器,只要它们具有不同的名称。我将进行快速编辑。进一步编辑定位器以消除任何歧义。