【问题标题】:How to select from a dropdown using POM Selenium如何使用 POM Selenium 从下拉列表中进行选择
【发布时间】:2015-01-29 13:45:30
【问题描述】:

所以我需要从下拉列表中进行选择,所有代码似乎都是突出显示值而不是选择它,

package uk.co.brightfuture.bfslogin.selenium.page;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

import uk.co.brightfuture.bfslogin.selenium.PagePath;
import uk.co.brightfuture.bfslogin.selenium.PageTitle;

public class CreateNewUserPage extends Page {

@FindBy(name = "firstname")
private WebElement firstNameTextBox;

@FindBy(name = "surname")
private WebElement surnameTextBox;

@FindBy(name = "phonenumber")
private WebElement phonenumberTextBox;

@FindBy(name = "username")
private WebElement userNameTextBox;

@FindBy(name = "password")
private WebElement passwordTextBox;

@FindBy(name = "next of kin firstname")
private WebElement nextOfKinFirstnameTextBox;

@FindBy(name = "next of kin surname")
private WebElement nextOfKinSurnameTextBox;

@FindBy(name = "next of kin phonenumber")
private WebElement nextOfKinPhonenumberTextBox;

@FindBy(name = "dropdown2")
private WebElement authorityDropdown;

@FindBy(name = "Admin")
private WebElement adminFromDropdown;

@FindBy(name = "User")
private WebElement userFromDropdown;

@FindBy(name = "Fire")
private WebElement fireMarshallFromDropdown;

@FindBy(name = "dropdown")
private WebElement groupDropdown;

@FindBy(name = "group")
private WebElement groupFromDropdown;

@FindBy(name = "add")
private WebElement addButton;

public CreateNewUserPage(WebDriver driver) {
    super(driver, PageTitle.CREATEUSER, PagePath.CREATEUSER);

    // COMPULSORY
    this.driver = driver;
    PageFactory.initElements(driver, this);

}

public void enterFirstName(String firstNameText) {
    firstNameTextBox.sendKeys(firstNameText);
}

public void enterSurName(String surnameNameText) {
    surnameTextBox.sendKeys(surnameNameText);
}

public void enterPhonenumber(String phonenumberText) {
    phonenumberTextBox.sendKeys(phonenumberText);
}

public void enteruserName(String userNameText) {
    userNameTextBox.sendKeys(userNameText);
}

public void enterpassword(String passwordText) {
    passwordTextBox.sendKeys(passwordText);
}

public void enternextOfKinFirstname(String nextOfKinFirstNameText) {
    nextOfKinFirstnameTextBox.sendKeys(nextOfKinFirstNameText);
}

public void enternextOfKinSurname(String nextOfKinSurnameText) {
    nextOfKinSurnameTextBox.sendKeys(nextOfKinSurnameText);
}

public void enternextOfKinPhonenumber(String nextOfKinPhonenumberText) {
    nextOfKinPhonenumberTextBox.sendKeys(nextOfKinPhonenumberText);
}

public void AuthorityDropDown() {
    authorityDropdown.click();
}

public  void adminViaDropDown() {
      Select drop = new Select(authorityDropdown);
      drop.selectByIndex(1);  
}

public void userFromDropdown() {
    userFromDropdown.click();
}

public void userFromDropDown() {
    fireMarshallFromDropdown.click();
}

public void GroupDropDown() {
    groupDropdown.click();
}

public Boolean getGroupDropown(String textToCheck) {
    return groupFromDropdown.getText().equals(textToCheck);
}

public NewUserConfirmationPage AddUserButton() {
    addButton.click();
    return new NewUserConfirmationPage(driver);
}

}

这是我要运行的测试,

    /**
 * this test will go to the new create new user page via the dash board and
 * it will enter the required data into the text fields.
 */

@Test
public void createNewUserWithCorrectData() {
    createNewUserPage = dashboardPage.clickCreateNewUserLink();
    assertTrue(isOnThisPage(PagePath.CREATEUSER));
    createNewUserPage.enterFirstName(FIRST_NAME);
    createNewUserPage.enterSurName(SURNAME);
    createNewUserPage.enterPhonenumber(PHONE_NUMBER);
    createNewUserPage.enteruserName(EMAIL);
    createNewUserPage.enterpassword(PASSWORD);
    createNewUserPage.enternextOfKinFirstname(NEXT_OF_KIN_FIRST_NAME);
    createNewUserPage.enternextOfKinSurname(NEXT_OF_KIN_SURNAME);
    createNewUserPage.enternextOfKinPhonenumber(NEXT_OF_KIN_PHONE_NUMBER);

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("window.scrollBy(0,250)", "");

    createNewUserPage.AuthorityDropDown();
    createNewUserPage.adminViaDropDown();
    createNewUserPage.GroupDropDown();
    createNewUserPage.getGroupDropown(GROUP);
    newUserConfirmationPage = createNewUserPage.AddUserButton();
    assertTrue(isOnThisPage(PagePath.CREATECONFIRMED));
}

但是我只能使用页面对象模型,因为我的经理不允许我们使用其他任何东西。

<body>
<div class="back">
    <div id="different">
        <form method="post">
            <div class="center">
                <h1 id="large">ADD NEW USER</h1>
            </div>
            <input type="string" class="form-control" name="firstname"
                placeholder="Firstname"> <input type="string"
                class="form-control" name="surname" placeholder="Surname">
            <input type="number" class="form-control" name="phonenumber"
                placeholder="Phonenumber"> <input type="string"
                class="form-control" name="username" placeholder="Username">
            <c:if test="${UserAlreadyExists}">
                <h6
                    style="margin-bottom: 10px; margin-top: -10px; margin-left: 14px; color: red;">User:${username},
                    already exists</h6>
            </c:if>
            <input type="password" class="form-control" name="password"
                placeholder="Password"> <input type="string"
                class="form-control" name="next of kin firstname"
                placeholder="Next of kin firstname"> <input type="string"
                class="form-control" name="next of kin surname"
                placeholder="Next of kin surname"> <input type="number"
                class="form-control" name="next of kin phonenumber"
                placeholder="Next of kin Phonenumber">

            <!-- this section is for the button and the drop down section -->
            <div class="centered">
                <div class="row">
                    <div id="groupMenu">
                        <div class="btn-group">
                            <form>
                                <select class="form-control" id="dropdown2"
                                    style="width: 160px;" name="dropdown2">
                                    <option value="feedback" name="choose">Select
                                        Authority</option>
                                    <option name="admin">Admin</option>
                                    <option name="user">User</option>
                                    <option name="fire">Fire Marshall</option>
                                </select> <select class="form-control" id="dropdown"
                                    style="width: 160px;" name="dropdown">
                                    <option value="feedback" name="choose">Select Group</option>
                                    <c:forEach items="${Groups}" var="group" varStatus="status">
                                        <option name="group" value="${group.id}">
                                            ${group.groupName}</option>
                                    </c:forEach>
                                </select>
                            </form>
                        </div>
                    </div>
                    <button class="btn btn-default" type="submit" name="add" id="btn1"
                        onclick="javascript: form.action='Submit';">ADD</button>
                </div>
            </div>
        </form>
    </div>
</div>

这是 HTML

【问题讨论】:

    标签: java selenium pageobjects


    【解决方案1】:

    你需要给我们stacktrace和

    public  void adminViaDropDown() {
          Select drop = new Select(authorityDropdown);
          drop.selectByIndex(1);  
    }
    

    只选择第一个索引的下拉菜单

    你应该这样做

    public  void adminViaDropDown(int index) {
          Select drop = new Select(authorityDropdown);
          drop.selectByIndex(index);  
    }
    

    使用:

    createNewUserPage.adminViaDropDown(1);
    

    您还可以通过多种方式选择下拉菜单。见this

    但是,如果元素是动态加载的,则有时元素映射技术无法按预期工作。在这种情况下,我会实时找到元素。

    public  void adminViaDropDown(int index) {
          WebEelement ele = driver.findElement("your by selector")
          Select drop = new Select(ele );
          drop.selectByIndex(index);  
    }
    

    而且,另一种可能性是等待。如果没有任何效果,我会在最后插入它。这是explicit wait

    public  void adminViaDropDown(int index) {
              WebElement myDynamicElement = (new WebDriverWait(driver, 10))
      .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));  
              Select drop = new Select(myDynamicElement );
              drop.selectByIndex(index);  
        }
    

    编辑:直接使用css找到选项

    //css
    public  void adminViaDropDownByName() {
        By byCss = By.cssSelector("#dropdown2>option[name='admin']");
        driver.findElement(byCss).click();      
    }
    

    第二次编辑 我不喜欢这里的HTML 设计。你应该和开发商谈谈。但是,我发现了一种非常糟糕的方法来处理这个问题。我不建议这样做。但只是一种解决方法。

    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("document.getElementById('dropdown2').querySelector('[name=admin]').setAttribute('selected','selected');");
    

    【讨论】:

    • 所以,我做了这些更改,但是添加等待和动态加载的更改也不起作用。但是使用索引它仍然只突出显示选择,我需要它来单击选择但似乎无法理解如何做到这一点。
    • 你能提供htmlsn-p吗?
    • 通常使用下拉元素,您只需“选择”一个选项,突出显示它。例如,通常您会单击其他内容以“确认”该选择。如果您的要求认为您需要直接单击您的选项,您会发现使用 Select() 类很棘手 - 因为这是选择而不是单击选项。您可以直接单击一个选项,使用普通定位器通过其实际 DOM 位置找到该选项,并在其上调用 .click() 方法。
    • 为了清楚起见,我添加了 hml,抱歉,DOM 位置是什么意思?
    • 我仍然遇到同样的问题,我将与开发人员讨论,看看他们是否可以整理他们的代码和其他东西,不过感谢您的帮助,我会的将此标记为正确,因为它似乎可以使用更好的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多