我正在尝试实现 Selenium Webdriver
使用 Java
.
基本上,我有一个空白字段的网站。用户单击该字段后,将出现一个包含 5 个选项的下拉列表,用户应选择一个选项。
代码是这样的
<!-- language: lang-html -->
<div class="default-form w-border scheduleAddFrom" style="display: block;">
<div>
<div class="section frameless nopadding nomargin" data-form-element="SectionHeading" style="min-width: 100%;">
<div class="section-body frameless nopadding nomargin">
<div class="default-form">
<div class="form-row required-message hidden" style="min-height: 25px;">
<div class="form-row print-avoid-page-break" data-form-element="FieldEdit" style="min-height: 25px;">
<label for="">Department</label>
<input id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display" class="ui-autocomplete-display validate widget" type="text" autocomplete="off">
<span class="ui-autocomplete-display-icon"></span>
<div class="subhidden">
<select id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId" class="validate widget " data-default-value="" tabindex="5000" data-display-id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display">
<option value=""></option>
<option value="OPT1">Option 1</option>
<option value="OPT2">Option 2</option>
<option value="OPT3">Option 3</option>
<option value="OPT4">Option 4</option>
<option value="OPT5">Option 5</option>
</select>
我尝试使用此 Java 代码来选择选项 2
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".form-row.print-avoid-page-break>label")));
//Start to find the element. The ID is dynamically randomly generated by the system each time the page loads except the last part TaskID, thus looking for the string TaskID
Select dropdown = new Select (driver.findElement(By.xpath(".//*[contains(@id,'TaskId')]")));
dropdown.selectByValue("OPT2");
Selenium
返回错误
org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
我感觉这是由 <div class="subhidden">
引起的,但我不太确定。
非常感谢任何建议。谢谢。
最佳答案
测试了下面的代码块,看起来不错。
看着 html
选择器不会唯一返回意图元素是一个合理的假设。我相信你想要 Select
包含 id
的标签TaskId
.只需使用 //select[contains(@id,'TaskId')]
进行标签相关搜索即可自 <input id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display"
也有相同文本的 ID TaskId
今后我建议您测试 xpath
在将其插入测试之前,至少根据我的经验来说。
WebDriverWait wait = new WebDriverWait(driver, 100);
By selectElementSelector = By.xpath("//select[contains(@id,'TaskId')]");
WebElement selectElement = wait.until(ExpectedConditions.presenceOfElementLocated(selectElementSelector));
Select dropdown = new Select (selectElement);
dropdown.selectByValue("OPT2");
编辑
另一种可能的选择是使用适当的选择器查找元素以及选项。同样,您必须测试选择器以确保它是唯一的并且是您想要的。
WebDriverWait wait = new WebDriverWait(driver, 100);
String optionToSelect = "OPT1";
//Selector is the trick here
By selectElementSelector = By.cssSelector("select[id*='TaskId']>option[value='" + optionToSelect + "']");
wait.until(ExpectedConditions.presenceOfElementLocated(selectElementSelector)).click();
关于java - Selenium Webdriver 无法在下拉列表中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29089945/