下拉selectByValue不能选择菜单项

时间:2018-12-07 15:37:48

标签: java selenium webdriver

Screenshot 我能够找到并单击元素,如下所示:

WebElement clienttype = driver.findElement(By.cssSelector("button[class='ui-button ui-widget ui-state-default ui-button-icon-only ui-corner-right ui-button-icon']"));
clienttype.click();

但是,当我尝试如下选择菜单项之一时:

Select dropdown = new Select(clienttype);
dropdown.selectByValue("PUBLIC-CUSTOMERS");

它只是绕过并继续脚本运行。我也尝试过selectByVisibleText,它也不起作用。有什么建议吗?

添加HTML ...

    <span class="ui-helper-hidden-accessible" role="status" aria-live="polite">8 results are available, use up and down arrow keys to navigate.</span>
    <button tabindex="-1" title="" class="ui-button ui-widget ui-state-default ui-button-icon-only ui-corner-right ui-button-icon" 
role="button" aria-disabled="false" type="button">
    <span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"></span>
    <span class="ui-button-text"></span>
    </button>
    </span>

更新的HTML显示菜单值>

<div class="questionContainer ">
<div class="left questionBody" id="QS005056A70C8E9da8UA3v-vDA7UcC-label-body" required="false">
<label class="questionlabel bold " id="QS005056A70C8E9da8UA3v-vDA7UcC-label" for="QS005056A70C8E9da8UA3v-vDA7UcC">
Client Type:<span class="required">*</span></label>
    </div>
<div class="left answerBody">
        <div class="left">     
<div id="QS005056A70C8E9da8UA3v-vDA7UcC-answer-body">          
<div class="select_holder select_jquery">              
<select tabindex="0" id="QS005056A70C8E9da8UA3v-vDA7UcC" aria-describedby="QS005056A70C8E9da8UA3v-vDA7UcC-instructions" 
aria-labelledby="QS005056A70C8E9da8UA3v-vDA7UcC-label" style="display: none;" required="true" level="0" autocomplete="off" 
responsetype="STATIC_MENU" questiondefid="QD005056A70C8E9da8UAyACwDA8EcC" totalorder="0" questionid="QS005056A70C8E9da8UA3v-vDA7UcC" val="" overwrite="1">                   
<option value=""></option>                                             
<option aria-selected="false" value="PUBLIC-CUSTOMERS" ps="0">PUBLIC-CUSTOMERS</option>                                               
<option aria-selected="false" value="Option 2" ps="1">Option 2</option>

<option aria-selected="false" value="Option 3" ps="2">Option 3</option>

<option aria-selected="false" value="Option 4 " ps="3">Option 4 </option>

<option aria-selected="false" value="Option 5" ps="4">Option 5</option>

<option aria-selected="false" value="Option 6" ps="5">Option 6</option>

<option aria-selected="false" value="Option 7" ps="6">Option 7r</option>

<option value="clear">(clear)</option>

</select><span><input class="ui-widget ui-widget-content ui-corner-left ui-autocomplete-input" id="QS005056A70C8E9da8UA3v-vDA7UcC_input" autocomplete="off">
<span class="ui-helper-hidden-accessible" role="status" aria-live="polite">8 results are available, use up and down arrow keys to navigate.</span>
<button tabindex="-1" title="" class="ui-button ui-widget ui-state-default ui-button-icon-only ui-corner-right ui-button-icon" role="button" aria-disabled="false" type="button">
<span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"></span><span class="ui-button-text"></span></button></span>

</div>

</div>

</div>

<div class="validationResult clear" id="QS005056A70C8E9da8UA3v-vDA7UcC-validationResult"></div>

<div class="clear"></div>

<div class="instructions" id="QS005056A70C8E9da8UA3v-vDA7UcC-instructions"></div>

</div>

<div class="clear"></div>

<div class="no_dispaly" id="QS005056A70C8E9da8UA3v-vDA7UcC-rules"></div>

<div class="no_dispaly" id="QS005056A70C8E9da8UA3v-vDA7UcC-json"></div>

</div>

1 个答案:

答案 0 :(得分:0)

您尚未传递选择类型元素。这就是为什么selectByValue无法正常工作的问题。

您发布的更新的HTML具有select元素。

对于此页面源,请将select元素传递给new Select()构造函数。然后,您可以使用selectByValue方法。

如果ID不是动态生成的,则clienttype变量应为:

WebElement clienttype = driver.findElement(By.id("QS005056A70C8E9da8UA3v-vDA7UcC"));

否则:

WebElement clienttype = driver.findElement(By.cssSelector("div.questionContainer > div.left.answerBody > div > div > div > select"));

然后您可以使用以下代码:

Select dropdown = new Select(clienttype);
dropdown.selectByValue("PUBLIC-CUSTOMERS");