如何使用webdriver选择下拉列表中的项目(使用或不使用xpath)

时间:2013-06-26 18:45:11

标签: java selenium selenium-webdriver

我在使用selenium webdriver和java选择下拉列表时也遇到了问题。

1)弹出窗口后,我必须单击下拉菜单并选择一个字段并选择下一个下拉菜单,然后再选择一个下拉列表并选择一个字段。 2)这些下拉也充当输入字段,所以我使用输入id作为我的xpath,它在框中键入而不是选择它。现在我不想去那条路。而不想从下拉列表中选择。

这是弹出窗口的HTML。任何想法??

这是我写的:

 WebElement orgUnitText = driver.findElement(By.xpath("//input[@id='textfield-1137-     inputEl']"));
    orgUnitText.sendKeys("HRD");
    logger.info("entered OrgUnitText");
    WebElement orgUnitAbb = driver.findElement(By.xpath("//input[@id='textfield-1138-  inputEl']"));
    orgUnitAbb.sendKeys("HRD");
    logger.info("entered OrgUnit abbreviation");
    Thread.sleep(15000);


    List<WebElement> options = driver.findElements(By.id("combobox-1140-inputEl"));
    options.get(0).sendKeys("HR_ADMINS");


    List<WebElement> options2 = driver.findElements(By.id("combobox-1141-inputEl"));
    options2.get(0).sendKeys("HR_USERS");

    WebElement textArea = driver.findElement(By.id("textareafield-1143-inputEl"));
    textArea.sendKeys("HRD");

    WebElement saveButton = driver.findElement(By.xpath("//a[@id='button-1146-btnEl']"));
    saveButton.click();
    logger.info("entered OrgUnit description");



</div>
<div id="orgunitpanel-1134-body" class="x-window-body x-window-body-default x-layout-fit x-closable x-window-body-closable x-window-body-default-closable x-window-body-default x-window-body-default-closable" style="background: none repeat scroll 0% 0% white; width: 690px; left: 0px; top: 20px; height: 178px;">
<div id="form-1135" class="x-panel x-fit-item x-window-item x-panel-default" style="padding: 5px; margin: 0px; width: 688px; height: 176px;">
<div id="form-1135-body" class="x-panel-body x-panel-body-default x-panel-body-default x-docked-noborder-top x-docked-noborder-right x-docked-noborder-bottom x-docked-noborder-left" style="width: 678px; left: 0px; top: 0px; height: 166px;">
<span id="form-1135-outerCt" style="display: table; width: 100%; table-layout: fixed;">
</div>
</div>
</div>
<div id="toolbar-1145" class="x-toolbar x-docked x-toolbar-footer x-docked-bottom x-toolbar-docked-bottom x-toolbar-footer-docked-bottom x-box-layout-ct" style="width: 690px; right: auto; left: 4px; top: 202px;">
<div id="toolbar-1145-innerCt" class="x-box-inner " role="presentation" style="width: 684px; height: 22px;">
<div id="toolbar-1145-targetEl" class="x-box-target" style="width: 684px;">
</div>
</div>
</div>
<div id="boundlist-1150" class="x-boundlist x-boundlist-floating x-layer x-boundlist-default x-resizable x-boundlist-resizable x-boundlist-default-resizable" tabindex="-1" style="right: auto; left: 1101px; top: 407px; width: 176px; z-index: 29001; height: auto; display: none;">
<div id="boundlist-1150-listEl" class="x-boundlist-list-ct x-unselectable" style="overflow: auto; height: auto;">
<div id="boundlist-1150-southeast-handle" class="x-resizable-handle x-resizable-handle-southeast x-boundlist-handle x-boundlist-handle-southeast x-boundlist-handle-southeast-br x-unselectable" unselectable="on"></div>
</div>
<div id="ext-gen1284" class="x-mask" style="z-index: 29001; width: 176px; height: 133px; right: auto; left: 1101px; top: 407px; visibility: hidden;"></div>
<div id="loadmask-1151" class="x-mask-msg x-layer x-mask-msg-default" style="right: auto; left: 1143px; top: 457px; z-index: 29003; display: none;">
<div id="loadmask-1151-msgEl" class=" x-mask-msg-inner">
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

只需使用包含WebElement的类Select并包含允许按值选择选项,可见文本,获取选项列表等的函数。