How to select options from 3 interlinked dropdowns with the same class name and different values using Selenium and Java

早过忘川 提交于 2021-01-07 06:57:06

问题


I have 3 options in a home page with the same class name and different values see code bellow:

First Option menu

 <select class="form-control mandatory" tabindex="4" required="" autocomplete="off" name="SearchHome.documentType">
    <option value=""></option>
    <option value="VISA">Visa</option>
    <option value="EDF">EDF</option>
    </select>

Second Option menu

<select class="form-control mandatory" tabindex="5" required="" autocomplete="off" name="SearchHome.documentSubType">
<option value=""></option>
<option value="BusinessVisa">Business Visa</option>
<option value="VisitantVisa">Visitant Visa</option>
<option value="WorkingVisa">Working Visa</option>
<option value="ResidenceVisa">Residence Visa</option>
<option value="StudentVisa">Student Visa</option>
<option value="TouristVisa">Tourist Visa</option>
<option value="VisaForTemporaryResidence">Visa for Temporary Residence</option>
<option value="VisaSportsCulture">Visa Sports + Culture</option>
<option value="VisaForInvestmentActivity">Visa For Investment Activity</option>
</select>

Third Option menu

<select class="form-control mandatory" tabindex="6" required="" autocomplete="off" name="SearchHome.useCaseName">
<option value=""></option><option value="EXTENSION">EXTENSION</option>
</select>

The values from the second and third option menu are dynamic. i.e. depending on what I choose in the first option menu the value in the second option menu will be different. And also depending on value I choose in the second option menu the value will be different on the third option menu.

I'm trying to automatically select the first option values (already did it), however when I try to select the second option values is always getting the first option values. :(

I tried to switch tab but this is not helping.

package co.edureka.selenium.inCountry;


import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.chrome.ChromeDriver;

public class BusinessVisaHappyPath {

static WebDriver driver;

public static void main(String[] args) {
        basicSettings();
        loginTestCase("getidadmin@boca01", "12345");
        selectVisaDocumentHomePage();       
        //CloseAndQuitWebDriver();
}


// this is working
private static void selectVisaDocumentHomePage() {
    // Wait until ExpectedConditions is Fulfilled
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy((By.xpath("//*[@class=\"form-control mandatory\"]"))));
    
    /*
     * Select dropDown = new
     * Select(driver.findElement(By.xpath("//*[@class=\"form-control mandatory\"]"))
     * ); dropDown.selectByValue("VISA"); WebElement element =
     * driver.findElement(By.xpath("//*[@class=\"form-control mandatory\"]"));
     * element.sendKeys(Keys.TAB);
     */
    
    
    WebElement mySelectElm = driver.findElement(By.xpath("//*[@class=\"form-control mandatory\"]")); 
    Select mySelect= new Select(mySelectElm);
    List<WebElement> options = mySelect.getOptions();
    for (WebElement option : options) {
        if (option.getText().equalsIgnoreCase("VISA")) {
            option.click();
        }
    }
    mySelectElm.sendKeys(Keys.TAB);
    
    String at = driver.getTitle();
    String et = "GetID Webg";
    
    
    if (at.equalsIgnoreCase(et))
    {
    
        System.out.println("Select Document Test, passed");
        selectVisaDocumentTypeHomePage();
        
    } else {
        System.out.println("Select Document Test, not passed");
    }
}

// second option is not working :(
private static void selectVisaDocumentTypeHomePage() { 
    // Wait until ExpectedConditions is Fulfilled
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    WebElement element =  driver.findElement(By.xpath("//*[@class=\"form-control mandatory\"]"));
    element.sendKeys(Keys.ENTER);
    
    /*
     * Select dropDown = new Select(driver.findElement(By.
     * xpath("//*[(@class=\"form-control mandatory\") and (@name=\"SearchHome.documentType\")]"
     * ))); dropDown.selectByValue("BusinessVisa");
     */
    WebElement mySelectElm = driver.findElement(By.xpath("//*[(@class=\"form-control mandatory\") and (@name=\"SearchHome.documentType\")]")); 
    Select mySelect= new Select(mySelectElm);
    
    System.out.println(mySelect.getOptions().get(1).getText());
    List<WebElement> options = mySelect.getOptions();
    for (WebElement option : options) {
        if (option.getText().equalsIgnoreCase("BusinessVisa")) {
            option.click();
            System.out.println("Select Document Type Test, passed");
        }
    }
    System.out.println("Select Document Type Test, not passed");
    
}

private static void loginTestCase(String login, String password){
    driver.findElement(By.xpath("//input[contains(@class, 'form-control margin-from-bottom')]")).sendKeys(login);
    driver.findElement(By.xpath("//input[contains(@class, 'inputMask text-center-sm form-control margin-from-bottom')]")).sendKeys(password);
    driver.findElement(By.xpath("//button[contains(@class, 'btn btn-default btn-block')]")).click();
    
    String at = driver.getTitle();
    String et = "GetID Webg";
    
    if (at.equalsIgnoreCase(et))
    {
        System.out.println("Login Test, passed");
        
    } else {
        System.out.println("Login Test, not passed");
    }
}

private static void basicSettings() {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\eclipse-workspace\\SeleniumProject\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      
      driver.get("http://10.35.1.4:10720/login/");
      
      //driver.manage().window().maximize();
}

// not using this method for now
private static void CloseAndQuitWebDriver() {
     driver.close();
     driver.quit();
    
}

How can I select the second and third dropdown tab with correct values ?


回答1:


Use name attribute in xpath which will uniquely identify the dropdown element.

Dropdown 1:

 //select[@name='SearchHome.documentType']

Dropdown 2:

//select[@name='SearchHome.documentSubType']

Dropdown 3:

//select[@name='SearchHome.useCaseName']

Effectively your code will be

WebElement mySelectElm1 = driver.findElement(By.xpath("//select[@name='SearchHome.documentType']")); 

WebElement mySelectElm2 = driver.findElement(By.xpath("//select[@name='SearchHome.documentSubType']")); 

WebElement mySelectElm3 = driver.findElement(By.xpath("//select[@name='SearchHome.useCaseName']")); 



回答2:


To select

  • VISA as the <option> from the first dropdown,
  • BusinessVisa as the <option> from the second dropdown,
  • EXTENSION as the <option> from the third dropdown,

You need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using cssSelector and selectByVisibleText():

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.form-control.mandatory[name='SearchHome.documentType']")))).selectByVisibleText("Visa");
    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.form-control.mandatory[name='SearchHome.documentSubType']")))).selectByVisibleText("Business Visa");
    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.form-control.mandatory[name='SearchHome.useCaseName']")))).selectByVisibleText("EXTENSION");
    
  • Using xpath and selectByValue():

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='form-control mandatory' and @name='SearchHome.documentType']")))).selectByValue("VISA");
    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='form-control mandatory' and @name='SearchHome.documentSubType']")))).selectByValue("BusinessVisa");
    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='form-control mandatory' and @name='SearchHome.useCaseName']")))).selectByValue("EXTENSION");
    


来源:https://stackoverflow.com/questions/65338836/how-to-select-options-from-3-interlinked-dropdowns-with-the-same-class-name-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!