问题描述
对于下面的 Inspected element Id ,我在UI屏幕上有几个字段.
I have the following Inspected element Id for a dropdown with a few fields in a UI screen.
DropDown值:
- 列出item1
- 列出item2
- 列出item3
已检查的元素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.
有人可以建议我如何将其纳入硒测试中吗?
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行中,根据下拉列表中的默认选项,应将第二个参数'number'替换为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从下拉菜单中选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!