首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Selenium选择第一个值

使用Selenium选择第一个值
EN

Stack Overflow用户
提问于 2018-08-20 14:03:48
回答 3查看 2.5K关注 0票数 1

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

代码语言:javascript
复制
<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>

已尝试的代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-08-20 14:14:43

下拉使用select和option标记。可以从selenium中使用select类。

代码语言:javascript
复制
Select dropdown = new Select(driver.findElement(By.cssSelector("select.form-control.input-sm")))  

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

或者使用value属性。

代码语言:javascript
复制
dropdown.selectByValue("PRIMARY-SECONDARY");  

编辑

您可以尝试使用以下代码:

代码语言:javascript
复制
Select dropdown = new Select(new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select.form-control.input-sm")))); 
dropdown.selectByVisibleText("Primary-Secondary (1:Many)");  
票数 2
EN

Stack Overflow用户

发布于 2018-08-21 04:47:49

你可以试试这个,

代码语言:javascript
复制
WebElement temp = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@class='form-control input-sm']")));
Select findValue= new Select(temp);
findValue.selectByValue("PARENT-CHILD");
票数 0
EN

Stack Overflow用户

发布于 2018-08-21 05:07:50

有许多方法已经提到,您可以使用不同的方法,如

代码语言:javascript
复制
    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)");

您可以使用selectByIndex(index)selectByValue(value)selectByVisibleText(text),但使用selectByValue(value)的下注方式是

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51932480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档