[英]Selenium Drop Down Box
I'm trying to automate account registration using Selenium java and I'm having troubles clicking a drop down menu on the registration. 我正在尝试使用Selenium java自动进行帐户注册,但是在单击注册上的下拉菜单时遇到了麻烦。 I want it to click the secret question drop down and select "Who is your favorite author?".
我希望它单击秘密问题下拉列表,然后选择“谁是您最喜欢的作者?”。 Here is the html code.
这是html代码。
<div class="clear input_wrapper_bg">
<div class="name_field">
<label for="SecretQuestion">Secret Question</label>
</div>
<div class="select_field">
<div class="select2-container" id="s2id_SecretQuestion">
<a href="#" onclick="return false;" class="select2-choice" tabindex="-1"><span>What is your mother's maiden name?</span><abbr class="select2-search-choice-close" style="display:none;"></abbr>
<div>
<b></b>
</div>
</a>
<div class="select2-drop select2-with-searchbox select2-drop-active select2-offscreen" style="display: block;">
<div class="select2-search">
<input type="text" autocomplete="off" class="select2-input select2-focused">
</div>
<ul class="select2-results">
</ul>
</div>
</div>
<select data-val="true" data-val-required="The Secret Question field is required." id="SecretQuestion" name="SecretQuestion" style="display: none;">
<option value="What is your mother's maiden name?">What is your mother's maiden name?</option>
<option value="What was your high school mascot?">What was your high school mascot?</option>
<option value="Who is your favorite author?">Who is your favorite author?</option>
<option value="What was the name of your first pet?">What was the name of your first pet?</option>
</select>
<div class="error_msg">
<span class="field-validation-valid" data-valmsg-for="SecretQuestion" data-valmsg-replace="true"></span>
</div>
</div>
</div>
I have tried using 我尝试使用
new Select(driver.findElement(By.id("s2id_SecretQuestion"))).selectByVisibleText("Who is your favorite author?");
But it doesn't work because when I click the drop down menu it adds some more html code at the bottom of the page. 但这是行不通的,因为当我单击下拉菜单时,它在页面底部添加了更多html代码。
<div class="select2-drop select2-with-searchbox select2-drop-active" style="display: block; top: 541.109375px; left: 876.3125px; width: 310px;">
<div class="select2-search">
<input type="text" autocomplete="off" class="select2-input select2-focused" tabindex="-1">
</div>
<ul class="select2-results">
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>What is your mother's maiden name?
</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>What was your high school mascot?
</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>Who is your favorite author?
</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted">
<div class="select2-result-label">
<span class="select2-match"></span>What was the name of your first pet?
</div>
</li>
</ul>
</div>
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks.
谢谢。
The way I interact with select lists is easy: 我与选择列表进行交互的方式很简单:
/**
* Clicks The Selector List and picks your option.
*
*/
public void selectOptionFromSelector(String selectedItem) {
WebElement select = webDriver.findElement(By.id("idhere"));
Select dropDown = new Select(select);
String selected = dropDown.getFirstSelectedOption().getText();
if(selected.equals(selectedItem)){
// This should not happen
}
List<WebElement> Options = dropDown.getOptions();
for(WebElement option:Options){
if(option.getText().equals(selectedItem)) {
option.click(); // clicks the option we want to select.
}
}
}
It appears you are using the wrong selector! 看来您使用的是错误的选择器! From the sample code you posted
By.id("s2id_SecretQuestion")
is a div
element and not a select
element. 在示例代码中,您通过
By.id("s2id_SecretQuestion")
是div
元素,而不是select
元素。 Try: 尝试:
new Select(driver.findElement(By.id("SecretQuestion"))).selectByVisibleText("Who is your favorite author?");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.