How can I use the dropdown option in Java with Selenium for Windows or macOS devices on Lambdatest?

How can I use the dropdown option in java using selenium for windows or macOS devices on Lambdatest? Could you please help me with any script for the same?

Hi Joe,

Here’s is how you can use the dropdown option in Java. Please refer to the below sample code:

package com.lambdatest;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class dropdown
{
    private RemoteWebDriver driver;
    private String Status = "failed";

    @BeforeMethod
    public void setup(Method m, ITestContext ctx) throws MalformedURLException {

        String username = "your_lambdatest_username";
        String authkey = "your_lambatest_access_key";
        String hub = "@hub.lambdatest.com/wd/hub";

        DesiredCapabilities caps = new DesiredCapabilities();
          caps.setCapability("platform", "MacOS Catalina");
          caps.setCapability("browserName", "Chrome");
          caps.setCapability("version", "104");
        caps.setCapability("build", "your build name");
        caps.setCapability("name", m.getName() + this.getClass().getName());
        caps.setCapability("plugin", "git-testng");
 

        String[] Tags = new String[]{"Feature", "Tag", "Moderate"};
        caps.setCapability("tags", Tags);

        driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps);

    }

    @Test
    public void basicTest() throws InterruptedException {
        String spanText;
        System.out.println("Loading Url");
        Thread.sleep(100);
        driver.manage().window().maximize();


        driver.get("https://onlinedegrees.purdue.edu/purdue-global/");
          Select Dropdown=  new Select(driver.findElement(By.xpath("//*[@id=\"aos\"]")));
            Dropdown.selectByVisibleText("Education");


             driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) ;
          Select Dropdown1 = new Select(driver.findElement(By.xpath("/html/body/main/form/section/div[2]/div/div/section[1]/section[1]/div[2]/select")));
    //  Select Dropdown1 = new Select(driver.findElement(By.xpath("//*[@id=\"degree\"]")));

            Dropdown1.selectByVisibleText("Associate's");
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);



          Select Dropdown2 = new Select(driver.findElement(By.xpath("//*[@id=\"program\"]")));

     Dropdown2.selectByVisibleText("Early Childhood Development");

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

       driver.findElement(By.id("btnContinue")).click();

        Status = "passed";
        Thread.sleep(800);
        System.out.println("TestFinished");
    }

    @AfterMethod
    public void tearDown() {
        driver.executeScript("lambda-status=" + Status);
        driver.quit();
    }
}