我想选择以下代码的第一个值:
<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)");
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>
最佳答案
下拉菜单是使用选择和选项标签制作的。您可以使用 selenium 中的选择类。
Select dropdown = new Select(driver.findElement(By.cssSelector("select.form-control.input-sm")))
dropdown.selectByVisibleText("Primary-Secondary (1:Many)");
或使用 value 属性。
dropdown.selectByValue("PRIMARY-SECONDARY");
编辑:
您可以尝试使用以下代码:
Select dropdown = new Select(new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select.form-control.input-sm"))));
dropdown.selectByVisibleText("Primary-Secondary (1:Many)");
关于java - 使用 Selenium 选择第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51932480/