我正在尝试使用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: element not visible: Element is not currently visible and may not be manipulated
我觉得这是由< div class =“subhidden”>引起的,但我不太确定.
任何建议都非常感谢.谢谢.
解决方法:
下面测试过的代码块看起来很好.
看看html,这是一个公平的假设,即选择器不会唯一地返回intendet元素.我相信你想要包含id TaskId的Select标签.只需简单地做一个依赖于标签的搜索//可以选择[包含(@ id,’TaskId’)],因为< input id =“Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-display”也有id与相同的文本TaskIdand继续我建议你在插入测试之前测试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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。