gpt4 book ai didi

java - Selenium 下拉框

转载 作者:行者123 更新时间:2023-11-30 08:22:21 25 4
gpt4 key购买 nike

我正在尝试使用 Selenium java 自动注册帐户,但在单击注册下拉菜单时遇到了问题。我希望它点击 secret 问题下拉菜单并选择“你最喜欢的作者是谁?”。这是 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>

我试过用

 new Select(driver.findElement(By.id("s2id_SecretQuestion"))).selectByVisibleText("Who is your favorite author?");

但它不起作用,因为当我单击下拉菜单时,它会在页面底部添加一些更多的 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>

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

我与选择列表交互的方式很简单:

/**
* 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.
}
}
}

关于java - Selenium 下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com