如何使用selenium和java从3个具有相同类名和不同值的链接下拉列表中选择选项

5q4ezhmt  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(440)

我在主页上有3个选项,它们具有相同的类名和不同的值,请参见下面的代码:
第一选项菜单

<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>

第二选项菜单

<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>

第三选项菜单

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

第二个和第三个选项菜单中的值是动态的。i、 e.根据我在第一个选项菜单中选择的内容,第二个选项菜单中的值将不同。根据我在第二个选项菜单中选择的值,第三个选项菜单中的值也会不同。
我正在尝试自动选择第一个选项值(已经选择了),但是当我尝试选择第二个选项值时,总是会得到第一个选项值:(
我试着换标签,但没用。

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();

}

如何使用正确的值选择第二个和第三个下拉选项卡?

d8tt03nd

d8tt03nd1#

使用 name attributexpath 它将唯一地标识下拉列表元素。
下拉列表1:

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

下拉列表2:

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

下拉列表3:

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

实际上你的代码

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']"));
f2uvfpb9

f2uvfpb92#

选择
visa作为 <option> 从第一个下拉列表中,
商务签证 <option> 从第二个下拉列表中,
扩展为 <option> 从第三个下拉列表中,
你需要诱导webdriverwait elementToBeClickable() 您可以使用以下任一定位器策略:
使用cssselector和 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");

使用xpath和 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");

相关问题