我有一个很长的 switch 语句代码(大约 8 种情况),它决定使用什么搜索来查找浏览器中的元素。有什么建议如何重构这段代码吗?
WebElement 当前对象 = null; switch (SearchBy) { case "className": try { CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT) .until(ExpectedConditions.presenceOfElementLocated(By.className(SearchPar))); } catch (Exception e) { System.out.println("未找到元素:" + e); } 休息;
case "cssSelector":
try {
CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT)
.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(SearchPar)));
} catch (Exception e) {
System.out.println("Element not found: " + e);
}
break;
case "id":
try {
CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT)
.until(ExpectedConditions.presenceOfElementLocated(By.id(SearchPar)));
} catch (Exception e) {
System.out.println("Element not found: " + e);
}
break;
case "linkText":
try {
CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT)
.until(ExpectedConditions.presenceOfElementLocated(By.linkText(SearchPar)));
} catch (Exception e) {
System.out.println("Element not found: " + e);
}
break;
case "name":
try {
CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT)
.until(ExpectedConditions.presenceOfElementLocated(By.name(SearchPar)));
} catch (Exception e) {
System.out.println("Element not found: " + e);
}
break;
default:
System.out.println(">>> SEARCH BY KEYWORD IS NOT VALID! <<<");
}
拉风的咖菲猫
温温酱
凤凰求蛊
相关分类