简体   繁体   English

使用Selenium WebDriver Java处理下拉列表

[英]Handling drop down using selenium webdriver java

I'm trying to do select options from a drop-down menu and here the source code: 我正在尝试从下拉菜单中选择选项,这里是源代码:

 <div class="multi ng-isolate-scope" isteven-multi-select="" input-model="genders" output-model="filter.genders" button-label="name" item-label="name" tick-property="ticked" on-select-all="search()" on-select-none="search()" on-item-click="search()" output-properties="id"> <span class="multiSelect inlineBlock buttonClicked"> <button id="" type="button" ng-click="toggleCheckboxes( $event ); refreshSelectedItems(); refreshButton(); prepareGrouping; prepareIndex();" ng-bind-html="varButtonLabel" ng-disabled="disable-button" class="ng-binding">None Selected<span class="caret"></span></button> <div class="checkboxLayer show"> <!-- ngIf: helperStatus.filter || helperStatus.all || helperStatus.none || helperStatus.reset --> <div class="helperContainer ng-scope" ng-if="helperStatus.filter || helperStatus.all || helperStatus.none || helperStatus.reset "> <!-- ngIf: helperStatus.all || helperStatus.none || helperStatus.reset --> <div class="line ng-scope" ng-if="helperStatus.all || helperStatus.none || helperStatus.reset "> <!-- ngIf: helperStatus.all --><button type="button" class="helperButton ng-binding ng-scope" ng-disabled="isDisabled" ng-if="helperStatus.all" ng-click="select( 'all', $event );" ng-bind-html="lang.selectAll">✓&nbsp;&nbsp;Select All</button> <!-- end ngIf: helperStatus.all --> <!-- ngIf: helperStatus.none --><button type="button" class="helperButton ng-binding ng-scope" ng-disabled="isDisabled" ng-if="helperStatus.none" ng-click="select( 'none', $event );" ng-bind-html="lang.selectNone">×&nbsp;&nbsp;Select None</button> <!-- end ngIf: helperStatus.none --> <!-- ngIf: helperStatus.reset --><button type="button" class="helperButton reset ng-binding ng-scope" ng-disabled="isDisabled" ng-if="helperStatus.reset" ng-click="select( 'reset', $event );" ng-bind-html="lang.reset">↶&nbsp;&nbsp;Reset</button> <!-- end ngIf: helperStatus.reset --> </div> <!-- end ngIf: helperStatus.all || helperStatus.none || helperStatus.reset --> <!-- ngIf: helperStatus.filter --> <div class="line ng-scope" style="position:relative" ng-if="helperStatus.filter"><input placeholder="Search..." type="text" ng-click="select( 'filter', $event )" ng-model="inputLabel.labelFilter" ng-change="searchChanged()" class="inputFilter ng-pristine ng-valid ng-touched" style=""><button type="button" class="clearButton" ng-click="clearClicked( $event )">×</button> </div> <!-- end ngIf: helperStatus.filter --> </div> <!-- end ngIf: helperStatus.filter || helperStatus.all || helperStatus.none || helperStatus.reset --> <div class="checkBoxContainer"> <!-- ngRepeat: item in filteredModel | filter:removeGroupEndMarker --> <div ng-repeat="item in filteredModel | filter:removeGroupEndMarker" class="multiSelectItem ng-scope vertical" ng-class="{selected: item[ tickProperty ], horizontal: orientationH, vertical: orientationV, multiSelectGroup:item[ groupProperty ], disabled:itemIsDisabled( item )}" ng-click="syncItems( item, $event, $index );" ng-mouseleave="removeFocusStyle( tabIndex );" style=""> <!-- ngRepeat: i in numberToArray( item[ spacingProperty ] ) track by $index --> <div class="acol"><label><input class="checkbox focusable" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event, $index )"><span ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, 'itemLabel' )" class="ng-binding">&nbsp;Male</span></label></div> <!-- ngIf: item[ groupProperty ] !== true && item[ tickProperty ] === true --> </div> <!-- end ngRepeat: item in filteredModel | filter:removeGroupEndMarker --> <div ng-repeat="item in filteredModel | filter:removeGroupEndMarker" class="multiSelectItem ng-scope vertical" ng-class="{selected: item[ tickProperty ], horizontal: orientationH, vertical: orientationV, multiSelectGroup:item[ groupProperty ], disabled:itemIsDisabled( item )}" ng-click="syncItems( item, $event, $index );" ng-mouseleave="removeFocusStyle( tabIndex );"> <!-- ngRepeat: i in numberToArray( item[ spacingProperty ] ) track by $index --> <div class="acol"><label><input class="checkbox focusable" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event, $index )"><span ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, 'itemLabel' )" class="ng-binding">&nbsp;Female</span></label></div> <!-- ngIf: item[ groupProperty ] !== true && item[ tickProperty ] === true --> </div> <!-- end ngRepeat: item in filteredModel | filter:removeGroupEndMarker --> <div ng-repeat="item in filteredModel | filter:removeGroupEndMarker" class="multiSelectItem ng-scope vertical" ng-class="{selected: item[ tickProperty ], horizontal: orientationH, vertical: orientationV, multiSelectGroup:item[ groupProperty ], disabled:itemIsDisabled( item )}" ng-click="syncItems( item, $event, $index );" ng-mouseleave="removeFocusStyle( tabIndex );"> <!-- ngRepeat: i in numberToArray( item[ spacingProperty ] ) track by $index --> <div class="acol"><label><input class="checkbox focusable" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event, $index )"><span ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, 'itemLabel' )" class="ng-binding">&nbsp;Not Recorded</span></label></div> <!-- ngIf: item[ groupProperty ] !== true && item[ tickProperty ] === true --> </div> <!-- end ngRepeat: item in filteredModel | filter:removeGroupEndMarker --> </div> </div> </span> </div> 

I have done a bit of research and they all use "By.id" but the source code didn't have id so I use xpath instead and still no result. 我做了一些研究,他们都使用“ By.id”,但是源代码没有id,所以我改用xpath,仍然没有结果。 I tried something like this: 我尝试过这样的事情:

Select gender = new Select(driver.findElement(By.xpath("//*[@id=\"offenderListView\"]/div/div[1]/ul/form/div[2]/span/button")));
    gender.selectByVisibleText("Male");

Here is the screenshot of the drop-down menu: 这是下拉菜单的屏幕截图: 性别下拉菜单

Thank you for your help! 谢谢您的帮助!

...... ......

Use browser's Developer Tools. 使用浏览器的开发人员工具。

If you are trying to learn XPATH, you can test your attempts in Developer Tools. 如果您尝试学习XPATH,则可以在Developer Tools中测试您的尝试。 Check that you get 1 hit only. 检查您是否只有1个命中。

Or you can find the XPATH using Developer Tools. 或者,您可以使用开发人员工具找到XPATH。 Ugly sometimes but functional. 有时很丑但是很实用。

First, find an XPath which will return you all values in the drop-down. 首先,找到一个XPath,它将在下拉菜单中返回所有值。 Then using List you can select any value. 然后,使用列表可以选择任何值。

List<WebElement> myElements = driver.findElements(mySelector);
   for(WebElement e : myElements) {
     if(e.getText().equalsIgnoreCase("Your_Desire_Value")) {
         e.click();
     }

Note:- click on the drop-down first using simple click operation and then use above code. 注意:-首先使用简单的单击操作单击下拉列表,然后使用上面的代码。 may you also need to apply focus if the value is present in deep below in the list. 如果该值位于列表的深处,则可能还需要应用焦点。

hope it will help you :) 希望它能对您有所帮助:)

As per the HTML you have shared you can't handle the Modal (Angular) Dropdown through Select as the options are within a <input> tag. 按照您共享的HTML ,由于选项位于<input>标记内,因此无法通过“ 选择”处理“ 模态(角度)”下拉 <input> To click on the Dropdown with text as Gender and then on the Checkbox with text as Male you can use the following code block : 要单击文本为Gender下拉列表 ,然后单击文本为Male复选框 ,可以使用以下代码块:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='multi ng-isolate-scope']//button[@class='ng-binding' and contains(normalize-space(), 'None Selected')]"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='multi ng-isolate-scope']//span[@class='ng-binding' and contains(.,'Male')]//preceding::input[1]"))).click();

In order to detect absolute Xpaths of your elements you can use fire-bug extentions firepath functionality. 为了检测元素的绝对Xpath,可以使用fire-bug扩展firepath功能。 As long as you don't have to use absolute xpath, go for other options, but sometimes using it solves your detection problem. 只要您不必使用绝对xpath,就可以使用其他选项,但是有时使用它可以解决检测问题。

Select elm = new Select(driver.findElement(By.Xpath("Absolute Xpath of your element")));
//Thread.sleep(100);
elm.selectByValue("Male");

Instead of using "//* [@id=\\"offenderListView\\"]/div/div[1]/ul/form/div[2]/span/button" , try to use absolute Xpath of your element. 代替使用“ // * [@id=\\"offenderListView\\"]/div/div[1]/ul/form/div[2]/span/button" ,请尝试使用元素的绝对Xpath。 (longest version of your xpath, not a dynamic one). (xpath的最长版本,而不是动态版本)。 In order to make it more see able put a thread of 100 milisec. 为了使它更多可见,可以放置一个100毫秒的线程。 between selecting element and selecting value from dropdown options. 在选择元素和从下拉选项中选择值之间。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM