Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

java - How to select an option from a dropdown through Selenium WebDriver

I have the following Inspected element Id for a dropdown with a few fields in a UI screen.

DropDown values:

  • List item1
  • List item2
  • List item3

Inspected Element 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.

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If a Drop Down is made of Select tag then you can use Select class of Selenium.

Select select = new Select(WebElement);
select.selectByIndex(int index);
select.selectByValue(String value);
select.selectByVisibleText(String text);  

If it is made of Divs and spans then you might wanna use this code :

List<WebElement> options = driver.findElements(by.xpath(" your locator"));
for(WebElement element : options){
 if(element.getText().equals(" your value from drop down")){
    element.click();
}
}

Update :

HTML File :

<html>
<head>
<title>StackOverFlow Problems </title>
</head>
<body>
<select id="form1:PartialSysAdminKey_adminContractIdField" name="form1:PartialSysAdminKey_adminContractIdField" class="selectOneMenu" size="1"> 
<option value=" "></option> 
<option value="Lstitem1">List item1</option> 
<option value="Lstitem2">List item2</option> 
<option value="Lstitem3">List item3</option> 
</select
</body>
</html>

Automation code using Java + Selenium :

public class Mike {

    static WebDriver driver;
    static WebDriverWait wait;

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "D:\Automation\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        wait = new WebDriverWait(driver, 20);
        driver.get("file:///C:/Users/HunteR/Desktop/Automation/abc.html");
        Thread.sleep(3000);
        Select select = new Select(driver.findElement(By.cssSelector("select[id*='adminContractIdField']")));
        select.selectByValue("Lstitem3");
        }
    } 

It is working mightily fine on my machine. Please let me know if you have any concerns related to this.

Note :
Thread.sleep(3000) was used in my code for visualization purpose.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...