【发布时间】:2015-05-19 08:12:07
【问题描述】:
我正在尝试使用 Java 实现 Selenium Webdriver。
基本上,我有一个空白字段的网站。用户单击该字段后,将出现一个包含 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:元素不可见:元素当前不可见,可能无法操作
我感觉这是<div class="subhidden">引起的,但我不太确定。
任何建议都受到高度赞赏。谢谢。
【问题讨论】:
-
您能否将 xpath 更改为 -
//select[contains(@id,'TaskId')]上面的输入标签在 id 中也有 TaskId。你可能是对的,选择在 div 下可能会被隐藏。
标签: java select selenium xpath webdriver