I have problem in getting the option value selected in dropdown with dynamic name and id. here's the html. Note: I will get the SELECTED OPTION VALUE from the dropdown. Not selecting a value on it.
I used many xpath contains but it doesn't works. the "[1]" changes everytime the page reloads.
Do you have any idea what is the correct xpath contains to use on this. BTW, I'm using web driver 2.32
<select name="this.is.dynamic.which.change.every.page.loads[1].select" id="this.is.dynamic.which.change.every.page.loads[1].select"
<option value> - Select </option>
<option value="1"> Option1</option>
<option value="2"> Option2</option>
<option value="3"> Option3</option>
<option value="4"> Option4</option>
<option value="5"> Option5</option>
<option value="6"> Option6</option>
</select>
It's a little bit tricky but my way to do that is :
//get the value
String name = driver.findElement(By.CssSelector(select)).getAttribute("name");
int flag = 0;
//loop for getting your dynamic number
for(int i=1; flag!=0; i++){
if (name.charAt(i).isNumeric()){
String num = name.charAt(i);
flag = 1;
}// end if
}//end for
// now get the value for the selected one
WebElement element = driver.findElement(By.CssSelector(select option:nth-child(num)));
It will probably not work (if you copy/past) because i can't test and i'm at work. But you got an idea to get what you want. Tell me what's up.
You can still use XPath Contains to match the Select then Option to Explicitly match your preferred option.
It will be Something like
//Select[contains(#name,'any frequent prefix')]/option[.='option']
or
//Select/option[.='option']
Hi Use the Select class in WebDriver API
String path = "/Select[contains(#name,'any frequet prefix')]"
Select options = new Select(driver.findElement(By.xpath(path)));
options.getAllSelectedOptions();
System.out.println(options.getAllSelectedOptions());
Can findElement by Tag if it's the Only Select/Dropdown on Page
Related
I'm trying to create an edit form for my laravel application that shows a list of damage types.
When the form loads, I want to have the value from the database selected (i.e. $device_destroyed[0]->damage_type)
However if the user submits the form and there are validation errors, I want the old value to be selected instead.
I've tried many different conditions but can't seem to get the logic right. I want to avoid duplicate values being displayed in the select options as well.
Hopefully the below might give an idea of what I'm trying to do:
<select id="damage_type" name="damage_type">
<option value="">Please Select</option>
#for ($i = 0 ; $i < count($damage_types); ++$i)
<option value="{{ $damage_types[$i] }}"
#if(old('damage_type') == $damage_types[$i])
selected
#endif
#if($damage_types[$i] == $device_destroyed[0]->damage_type)
selected
#endif
>{{ $damage_types[$i]}}
</option>
#endfor
</select>
Thanks to #Adrian Edy Pratama
for helping arrive at the solution.
Without the extra !($errors->any()) parameter in the elseif condition block, two select options were being selected.
The final solution was:
#if($errors->any() && old('damage_type') == "$damage_types[$i]")
selected
#elseif(!($errors->any()) && ($damage_types[$i] == $device_destroyed[0]->damage_type))
selected
#endif
>{{ $damage_types[$i]}}
Try this
#if($errors->any() && old('damage_type') == $damage_types[$i])
selected
#elseif($damage_types[$i] == $device_destroyed[0]->damage_type)
selected
#endif
I'm trying to get non-blank elements in my select using filter, but this doesn't work properly.
<select ng-model="selectedSubject.id">
<option value="0">
<%= res.getString("portlet.form.subjectField.default") %>
</option>
<option ng-repeat="sujet in subjects.allSubjects | filter:{name_fr:'!!'}" value="{{sujet.id}}">
{{sujet.name_fr}}
</option>
</select>
filter:{my_field:'!!'} will filter out only null values.
If you want to filter out empty values, or both null and empty, you should use a custom filter
<option ng-repeat="sujet in subjects.allSubjects | filter:notEmptyOrNull" value="{{sujet.id}}">{{sujet.name_fr}}</option>
Controller
$scope.notEmptyOrNull = function(item){
return !(item.name_fr === null || item.name_fr.trim().length === 0)
}
The filter that you are using will only filter the data that contains null values not blank.
To filter out blank data you have to create filter.This question has already been answered on stack overflow. Visit this link.
Angular - Filter to remove blank strings from array
You have to set the model so that angular selects the right option base on the select ngModel
<select ng-model="selectedSubject.id" ng-options="sujet.id as sujet.name_fr in subjects.allSubjects track by sujet.id"></select>
And set the default id on the controller.
$scope.allSubjects = subjects;
$scope.selectedSubject.id = subjects[0].id;
Unable to Select the Value Test3 from the dropdown in selenium Webdriver
Here is the HTML source of the Drop down
> <tr><td>Case Categories</td>
<td><select class="chzn-select" multiple="multiple" style="width: 500px" data-placeholder="Select categories..." size="4" name="categories[]" id="categories">
<option value="2">Test1</option>
<option value="3">Test2</option>
<option value="4">Test3</option>
<option value="1">Test</option>
</select></td></tr>
below is the selenium code written to select the value
`driver.findElement(By.cssSelector("input.default")).click();
driver.findElement(By.cssSelector("a.search-choice-close")).click();`
el = driver.find_element_by_id('categories')
for option in el.find_elements_by_tag_name('option'):
if option.get_attribute("value") == "4":
option.click()
Please use the below code:
Select dropDown = new Select(driver.findElement(By.id("categories")));
dropDown.selectByVisibleText("Test3");
Or
dropDown.selectByValue("4");
Here you can use xpath to identify the element and perform the required action on it
String xpath="//tr/td[2]/select/option[3]";
(or) xpath="//tr/td[2]//option[3]";
driver.findElement(By.xpath(xpath)).click();
you can use different criteria based xpaths to find the element, you can find most of the elements on any page using xpaths
I have the following object:
defaults = {
0 : {
id : 10,
value : "string 1"
},
1 : {
id : 22,
value : "string 2"
}
}
I want to iterate over this object and display it as a select box.
<select ng-model="selectedValue" ng-options="obj.id as obj.value for obj in defaults">
<option value="">---Select option-----</option>
</select>
What I'm trying to achieve is to select 'string 2' by default. But it does not work.
The problem I have is the fact the selectedValue is a string:
$scope.selectedValue = "string 2";
From what I learned from the documentation the selectedValue must be the same object. But unfortunately, it is not possible. In my case selectedValue must be a string.
In other words I need to operate with the name of the option only, I don't care about id.
I tried to use ng-repeat. It even works but displays an empty option and I don't think using ng-repeat is a good way to do it.
Any advice will be appreciated greatly.
If you want the selectedValue variable to contain the value, and not the ID, then you should not use obj.id as obj.value, but obj.value as obj.value.
Selected value should be id;
$scope.selectedValue = 22;
See Demo
EDIT:
change template
<select ng-model="selectedValue" >
<option value="">---Select option-----</option>
<option ng-selected="selectedValue == obj.value" value="{{obj.value}}" ng-repeat="obj in defaults">{{obj.value}}</option>
</select>
see Updated Demo
I think you misinterpreted the documentation.
Selected value should refer to a value of the same object, not be an object itself.
Thus, you can set the default value as :
$scope.selectedValue = $scope.defaults[1].value;
That way, the default value will be set - it does not matter if the value is a string or a number.
If you are using ng-repeat with options you need to use on-last-repeat on your options tag. For example
select name="repeatSelect" id="repeatSelect" ng-model="usertypes.SelectedId"
option ng-repeat="option in usertypes.AvailableOptions" value="{{option.Id}}" ng-selected="option.Id === usertypes.SelectedId" on-last-repeat>{{option.Name}}
select
Assume I have this html code:
<select id="superior" size="1" name="superior">
<option value=""></option>
<option value="c.i.e.m.md.Division_1">DIVISION007</option>
<option selected="selected" value="c.i.e.m.md.Division_$$_javassist_162_119">MyDivision</option>
<option value="c.i.e.m.md.Division_121">MyDivision4</option>
<option value="c.i.e.m.md.Division_122">MyDivision5</option>
</select>
So this is a combo box with
id=superior
and currently value MyDivision is selected.
Using Selenium WebDriver I am trying to get the selected value, but no success.
I tried:
String option = this.ebtamTester.firefox.findElement(By.id(superiorId)).getText();
return option;
But this returns me all the values in the combobox.
Help please?
Edit:
WebElement comboBox = ebtamTester.firefox.findElement(By.id("superior"));
SelectElement selectedValue = new SelectElement(comboBox);
String wantedText = selectedValue.getValue();
This is written in C#, but it shouldn't be hard to transition it over to any other language you're using:
IWebElement comboBox = driver.FindElement(By.Id("superior"));
SelectElement selectedValue = new SelectElement(comboBox);
string wantedText = selectedValue.SelectedOption.Text;
SelectElement requires you to use OpenQA.Selenium.Support.UI, so at the top, type
using OpenQA.Selenium.Support.UI;
Edit:
I suppose for you, instead of 'driver' you would use
IWebElement comboBox = this.ebtamTester.firefox.FindElement(By.Id("superior"));
In Java, the following code should work nicely:
import org.openqa.selenium.support.ui.Select;
Select comboBox = new Select(driver.findElement(By.id("superior")));
String selectedComboValue = comboBox.getFirstSelectedOption().getText();
System.out.println("Selected combo value: " + selectedComboValue);
As MyDivision is selected currently, the above code would print "MyDivision"
selectedValue.SelectedOption.Text; will get you the text of the
selected item. Does anyone know how to get the selected value.
To get the selected value use
selectedValue.SelectedOption.GetAttribute("value");
To select an option based on the label:
Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");
To get the first selected value:
WebElement option = select.getFirstSelectedOption()
Using XPath in c#
string selectedValue=driver.FindElement(By.Id("superior")).FindElements(By.XPath("./option[#selected]"))[0].Text;
Based off #Micheal's answer this would be easier to work with following command:
string selectedValue = new SelectElement(driver.FindElement(By.Id("Year"))).SelectedOption.Text;