简体   繁体   English

用硒选择第一个值

[英]Select the first value with Selenium

I would like to select the first value of the following code: 我想选择以下代码的第一个值:

<select class="form-control input-sm">
    <option value="PARENT-CHILD">Parent-Child (1:Many)</option>
    <option value="PRIMARY-SECONDARY">Primary-Secondary (1:Many)</option
    <option value="ASSOCIATED-TO">Associated To (Many:Many)</option>
</select>

Code Tried : 尝试过的代码:

driver.findElement(By.xpath("//*[@id=\"popover162353\"]/div[2]/div/form/div/div[1]/div[1]/select")).click();

new Select(driver.findElement(By.xpath("//*[@id=\"popover162353\"]/div[2]/div/form/div/div[1]/div[1]/select"))).selectByVisibleText("Parent-Child (1:Many)");

Html: HTML:

<div class="popover-content">
<div>
    <div class="editableform-loading" style="display: none;"></div>
    <form class="form-inline editableform" style="">
        <div class="control-group form-group">
            <div>
                <div class="editable-input">
                    <select class="form-control input-sm">
                        <option value="PARENT-CHILD">Parent-Child (1:Many)</option>
                        <option value="PRIMARY-SECONDARY">Primary-Secondary (1:Many)</option>
                        <option –value="ASSOCIATED-TO">Associated To (Many:Many)</option>
                    </select>
                </div>
                <div class="editable-buttons">
                    <button type="submit" class="btn btn-primary btn-sm editable-submit"><i class="glyphicon glyphicon-ok"></i></button>
                    <button type="button" class="btn btn-default btn-sm editable-cancel"><i class="glyphicon glyphicon-remove"></i></button>
                </div>
            </div>
            <div class="editable-error-block help-block" style="display: none;"></div>
        </div>
    </form>
</div>

Drop down is made using select and option tag. 使用选择和选项标签进行下拉。 You can use select class from selenium. 您可以使用硒中的选择类。

Select dropdown = new Select(driver.findElement(By.cssSelector("select.form-control.input-sm")))  

dropdown.selectByVisibleText("Primary-Secondary (1:Many)");  

or using value attribute. 或使用值属性。

dropdown.selectByValue("PRIMARY-SECONDARY");  

EDIT : 编辑

You may try with this code : 您可以尝试使用以下代码:

Select dropdown = new Select(new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select.form-control.input-sm")))); 
dropdown.selectByVisibleText("Primary-Secondary (1:Many)");  

You may try this, 你可以试试看

WebElement temp = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@class='form-control input-sm']")));
Select findValue= new Select(temp);
findValue.selectByValue("PARENT-CHILD");

There are many ways already mention , you can use different methods like 已经提到了很多方法,可以使用不同的方法,例如

    WebElement dropdownEle = driver.findElement(By.xpath("//select[@class='form-control input-sm']"));
    Select Dropdown = new Select(dropdownEle);
    Dropdown.selectByIndex(1);
    //Dropdown.selectByValue("Parent-Child (1:Many)");
    //Dropdown.selectByValue("Primary-Secondary (1:Many)");

You can use selectByIndex(index) or selectByValue(value) or selectByVisibleText(text) , but the bet way to use it using selectByValue(value) 您可以使用selectByIndex(index)selectByValue(value)selectByVisibleText(text) ,但是使用selectByValue(value)的下注方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM