我有以下 Inspected element Id 用于 UI 屏幕中带有几个字段的下拉菜单.
I have the following Inspected element Id for a dropdown with a few fields in a UI screen.
下拉值:
检查的元素 ID:
<select id="form1:PartialSysAdminKey_adminContractIdField" name="form1:PartialSysAdminKey_adminContractIdField" class="selectOneMenu" size="1">
在某些情况下,下拉列表将不包含任何值.
There will be cases when the drop down will hold no values.
我需要显示一个 sysout 日志,只有当这个下拉列表有至少一个值时.
I need to display a sysout log, only when this drop down has atleast one value.
有人可以建议我如何将其纳入我的 Selenium 测试吗?
Can someone please suggest how can I incorporate this in my Selenium testing?
目前,我有以下代码来检查服务器是否已启动并测试登录.
Currently, I have this following code that checks whether the server is up and tests a login.
package testPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class TestClass {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
System.setProperty("webdriver.chrome.driver", "D:\Softwares\chromedriver_win32\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://bigdata/steward.jsp");
if (driver.getTitle().equalsIgnoreCase("Login")) {
serverStatus = "UP";
} else {
serverStatus = "DOWN";
}
System.out.println("Server is " + serverStatus + ".");
if (serverStatus.equalsIgnoreCase("UP")) {
driver.findElement(By.id("username")).sendKeys("username");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("login")).click();
String newUrl = driver.getCurrentUrl();
if (newUrl.equalsIgnoreCase("https://bigdata/error.jsp")) {
System.out.println("Incorrect username/password.");
} else {
System.out.println("Logged in successfully.");
}
} else {
System.out.println("Login could not be done.");
}
driver.quit();
}
}
根据上述案例,可以使用以下代码检查下拉列表中的元素数量并相应地打印 sysout 日志.在 wait.until 行中,根据下拉菜单中的默认选项,第二个参数数字"应替换为 0 或 1.即如果下拉列表为空,则使用 0.如果下拉列表有默认选项--Select--",则使用 1.所以基本上你要做的是,等待下拉列表中是否有任何选项,而不是默认内容.您可以根据您的应用程序加载时间更改等待时间.
As per the case mentioned, following code can be used to check the number of elements in the dropdown and print sysout log accordingly. In the wait.until line, the 2nd argument 'number' should be replaced with either 0 or 1 according to the default options present in the dropdown. i.e If dropdown is empty use 0. If the dropdown has a default option as '--Select--', then use 1. So basically what you will be doing is, waiting if there is any option in the dropdown, more than the default content. You can change the wait time according to your application load time.
try {new WebDriverWait(driver, 15).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//select[@id='form1:PartialSysAdminKey_adminContractIdField']/option"), number));
System.out.println("Drop down has at least one value present");
} catch (Exception e) {
System.out.println("No options in the drop down");
}
另一种方式可能是
List<WebElement> list_Items=driver.findElements(By.xpath("//select[@id='form1:PartialSysAdminKey_adminContractIdField']/option"));
if(list_Items.size()>1){
System.out.println("Drop down has at least one value present");
}
else{
System.out.println("No options in the drop down");
}
这篇关于如何通过 Selenium WebDriver 从下拉列表中选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!