简体   繁体   English

如何使用webdriver(使用或不使用xpath)在下拉菜单中选择项目

[英]How to select an item in the drop down using webdriver (with or without using xpath)

I have an issue while selecting a dropdown which is also an input field using selenium webdriver and java. 选择下拉菜单(这也是使用Selenium Webdriver和Java的输入字段)时出现问题。

1) After the pop up appears, I will have to click the drop down and select a field and tab to the next drop down and select one more drop down and select a field . 1)弹出窗口出现后,我将必须单击下拉列表,然后选择一个字段,然后跳至下一个下拉列表,然后再选择一个下拉列表,然后选择一个字段。 2) These drop downs also act as input fields, so I used the input id for my xpath and it does type in the box instead of selecting it. 2)这些下拉菜单也充当输入字段,因此我将输入id用作xpath,它确实在框中键入而不是选择它。 Now I don't want to go that route.Instead want to select from the dropdown itself. 现在我不想走那条路线,而是想从下拉菜单中选择。

Here is the HTML of the pop up. 这是弹出窗口的HTML。 ANy ideas?? 有任何想法吗??

Here is what I have written: 这是我写的:

 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>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有 Java 的 Selenium WebDriver 从下拉列表中选择项目? - How to select item from drop down list using Selenium WebDriver with Java? 使用以下命令从Selenium Java Webdriver下拉菜单中选择一个值 - select a value from drop down in selenium java webdriver using 使用selenium webdriver在firefox 48中选择下拉列表不起作用 - Select drop down is not working in firefox 48 using selenium webdriver 无法使用 webdriver 中的操作 class 从下拉列表中 select 元素 - Unable to select element from drop down using Actions class in webdriver 按钮ng下拉菜单-如何在Java中使用WebDriver选择项目 - Button ng drop down- how to choose the item using webdriver with java 如何使用带有Java的Selenium WebDriver从下拉列表中选择一个选项? - How to select an option from a drop-down using Selenium WebDriver with Java? 如何使用selenium webdriver java从城市字段的GoIbibo中的自动选择下拉列表中选择一个元素 - how to select an element from autoselect drop down in GoIbibo from city field using selenium webdriver java 如何使用python或java在selenium webdriver中选择kendo-drop - how to select kendo-drop down in selenium webdriver using python or java 如何使用Selenium Webdriver从google注册页面的下拉菜单中选择Birthday(Month)? - How to select Birthday(Month) from drop down in the google Sign up page by using Selenium Webdriver? 如何从使用Selenium Webdriver中的api响应动态启动的下拉列表中选择特定位置? - How to select particular location from the drop down list which is initiated dynamically using api response in selenium webdriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM