使用 Selenium 选择第一个值

我想选择以下代码的第一个值:


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

代码尝试:


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

网址:


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


catspeake
浏览 336回答 3
3回答

慕的地6264312

下拉是使用选择和选项标签进行的。您可以使用 selenium 中的 select 类。Select dropdown = new Select(driver.findElement(By.cssSelector("select.form-control.input-sm")))&nbsp;&nbsp;dropdown.selectByVisibleText("Primary-Secondary (1:Many)");&nbsp;&nbsp;或使用 value 属性。dropdown.selectByValue("PRIMARY-SECONDARY");&nbsp;&nbsp;编辑:您可以尝试使用此代码:Select dropdown = new Select(new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select.form-control.input-sm"))));&nbsp;dropdown.selectByVisibleText("Primary-Secondary (1:Many)");&nbsp;&nbsp;

饮歌长啸

已经提到了很多方法,您可以使用不同的方法,例如&nbsp; &nbsp; WebElement dropdownEle = driver.findElement(By.xpath("//select[@class='form-control input-sm']"));&nbsp; &nbsp; Select Dropdown = new Select(dropdownEle);&nbsp; &nbsp; Dropdown.selectByIndex(1);&nbsp; &nbsp; //Dropdown.selectByValue("Parent-Child (1:Many)");&nbsp; &nbsp; //Dropdown.selectByValue("Primary-Secondary (1:Many)");您可以使用selectByIndex(index)orselectByValue(value)或selectByVisibleText(text),但最好的使用方式是使用selectByValue(value)

慕沐林林

你可以试试这个WebElement temp = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@class='form-control input-sm']")));Select findValue= new Select(temp);findValue.selectByValue("PARENT-CHILD");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java