i tried to select a value out of a combo box via groovy(geb).
the html code is:
<select id="entity-list-form:statusSearchBtn" name="entity-list-form:statusSearchBtn" size="1" style="width: 200px;">
<option value="">alle</option>
<option value="REGISTERED" selected="selected">Wartet auf Bestätigung</option>
<option value="REJECTED">Registrierung zurückgewiesen</option>
<option value="APPROVED">Registrierung angenommen</option>
<option value="UNSUBSCRIBED">Abgemeldet</option>
</select>
i tried to access these elements via
$("entity-list-form").statusSearchBtn = "alle"
or
$("entity-list-form").statusSearchBtn.value() == "alle"
a different approach was in the page siet to add
statusSearchBtn { $('select[name$="entity-list-form:statusSearchBtn"]') }
and also the case with the name only like entity-list-form. in this caes i tried it like
statusSearchBtn = "alle"
or
statusSearchBtn.value() == "alle"
the last one end without any errors, but didnt change the selected value to "alle".
the former one ended in No such property: statusSearchBtn for class: geb.navigator.EmptyNavigator.
i greatly apprichiate any advice,
I think:
$("select", name : "entity-list-form:statusSearchBtn").value('alle')
$("select", name : "entity-list-form:statusSearchBtn").value() = 'alle'
Related
I have a list of suppliers and a defaultSupplier assigned to a template, I add the suppliers to a dropdown and iterate through them using a foreach and I try to make the default selected value depending on the defaultSupplier like this:
<select name="supplier_id" id="supplier_id" class="selectpicker" data-live-search="true" data-width="100%" data-show-tick="false">
<option value="" {if $defaultSupplier eq ""}selected="selected"{/if}>-- {"Choose a supplier"|t} --</option>
{foreach $suppliers as $s}
<option value="{$s.supplier_id}" {if ($defaultSupplier == $s.supplier_name)}selected="selected"{/if}>{$s.supplier_name}</option>
{/foreach}
</select>
But it doesn't work, I get the "Choose a supplier" option selected then I have to manually select my supplier from the dropdown
I had to close the page and navigate on it again and it worked
I need the value that is obtained from my db is selected. Inspect in html, I see that the value of one of the <option> changes to: selected="selected" but in the select it is not really selected.
Here is my code:
<select chosen class="form-control" name="state" ng-model="product.state" ng-change="updateData()" placeholder-text-single="'Select one'" required >
<option value="">Select one</option>
<option ng-repeat='i in statesList' value="{{i.id}}" ng-selected="i.id == product.state.id">{{i.name}}</option>
</select>
I tried to change the value of ng-model by ng-model="product.state.id" and it works, it stays selected, but of course, the data is not saved later.
statesList is the array of objects that are retrieved from a mongodb database collection.
I have read other similar questions but I can not solve my problem with them. I am new at angular and there are complicated problems for me. What solution is there? How can i fix it?
No need to have ng-change handler.
Try below approach by using ng-options. The model will always have latest selected value.
<select name="state" id="state"
ng-options="option.name for option in statesList track by option.id"
ng-model="product.state"></select>
In my project i have handled selects control with Reactive form, you can try like this
this.testForm = this.formBuilder.group({
"testselectControl": []});
in html
<select id="test1" formControlName="testselectControl"
class="form-control p-0 start-date-input-box ">
<option *ngFor="let val of listValues" [value]="val.id">
{{name.name}}</option>
</select>
when you receive data set value like this
this.testForm .get("testselectControl").setValue(listValues ? (listValues.find((value) => (value.id.trim() ==selectedValueId) : "0");
<select multiple ui-select2="{allowClear: true}" ng-
model="selectedCountries" data-placeholder="Countries">
<option ng-repeat="country in countries" value="{{country.id}}">
{{country.name}}</option>
</select>
<button ng-click="fetch()" class="btn btn-primary pull-right">Apply</button>
the solution of adding priority worked
I have been researching for a way to change the element of a web page and have yet found (or understood) how to do it.
In the above element I want to be able to change numerical value of to another number that is represented by a placeholder (i.e. nod for number of draws. Or do I have to do it manually every time I run the program?).
My OS is Ubunt 18.04 and using latest python, selenium and pycharm IDE.
These are just a couple of the websites I have visited and I have visited a lot.
https://www.youtube.com/watch?v=tdgAIDR9snc
http://allselenium.info/javascript-using-python-selenium-webdriver/
Set value of input instead of sendKeys() - selenium webdriver nodejs
WEBELEMENT:
<select name="ctl00$MainContent$ddlRange" id="ctl00_MainContent_ddlRange">
<option selected="selected" value="1"> Single Draw</option>
<option value="2"> 2 Consecutive</option>
<option value="3"> 3 Consecutive</option>
<option value="4"> 4 Consecutive</option>
<option value="5"> 5 Consecutive</option>
<option value="10">10 Consecutive</option>
<option value="20">20 Consecutive</option>
</select>
And my basic code is:
from Selenium import WebDriver
browser = webdriver.Chrome('/usr/bin/chromedriver')
page=browser.get("whatever address of whatever webpage interested in")
elem=browser.find_element_by_css_selector("#ctl00_MainContent_ddlRange")
browser.execute_script("arguments[0].setAttribute('<option selected="selected" value="20">','20 Consecutive</option>')", elem)
My goal is to change the value number in the first part of the option tag from:
<option selected="selected" value="1"> Single Draw</option>
to this:
<option selected="selected" value="44"> Single Draw</option>
In the execute_script I am always getting end of statement expected after the last ).
I am also getting errors of ',' or ')' expected in the areas of "selected" and "20"
Also the css and xpath selectors to these are:
for <select name="ctl00$MainContent$ddlRange" id="ctl00_MainContent_ddlRange">
selector = #ctl00_MainContent_ddlRange
xpath = //*[#id="ctl00_MainContent_ddlRange"]
for <option>
selector = #ctl00_MainContent_ddlRange > option:nth-child(1) (note that nth-child() can be 1 - 7)
xpath = //*[#id="ctl00_MainContent_ddlRange"]/option[1] (note that option[] can also be 1 - 7)
I have the webpage saved to disc and I can go into the html and change that one number then save the file and when I click on it works. I just want to be able to automate it.
Any help is much appreciated
Idzireit
EDIT:
So far this is the closest I have come to actually inserting the value I want. The new code is as follows:
browser.execute_script("""arguments[0].setAttribute
('select', '<option selected="selected" value="nod"> &snbsp;draws </option>')""", get_draws)
Results in this:
<option value="20" select="<option selected="selected" value="nod"> &snbsp;draws </option>">20 Consecutive</option>
The javascript I am trying to inject is getting injected in the middle of the element I am trying to modify (notice the option sequence repeats in the middle of the first bracket).
How can I correct this to make it look like this:
<option selected="selected" value="a number I am trying to change"> Draw<\option>
EDIT SOLVED:
Figured out how to change the value of an element. I had to change the search method of that element to this:
get_draws = browser.find_element_by_tag_name('option')
Then my next line was pretty simple:
browser.execute_script("""arguments[0].setAttribute('value', '100')""", get_draws)
For your second question, parametrizing the int value, try the following:
var = 100
js = f"arguments[0].setAttribute('value', '{var}')"
browser.execute_script(js, get_draws)
Just pass the value you want to the var variable.
I'm a little bit confused with Angular and ng-options.
I have a simple array and I want to init a select with it. But, I want that options value = label.
script.js
$scope.options = ['var1', 'var2', 'var3'];
html
<select ng-model="myselect" ng-options="o for o in options"></select>
What I get:
<option value="0">var1</option>
<option value="1">var2</option>
<option value="2">var3</option>
What I want:
<option value="var1">var1</option>
<option value="var2">var2</option>
<option value="var3">var3</option>
So I tried:
<select ng-model="myselect2" ng-init=0 ng-options="options[k] as v for (k,v) in options"></select>
<select ng-model="myselect3" ng-init=0 ng-options="b as b for b in options"></select>
(But it didn’t work.)
Edit:
My form is submitted externally, which is why I need 'var1' as the value instead of 0.
You actually had it correct in your third attempt.
<select ng-model="myselect" ng-options="o as o for o in options"></select>
See a working example here:
http://plnkr.co/edit/xEERH2zDQ5mPXt9qCl6k?p=preview
The trick is that AngularJS writes the keys as numbers from 0 to n anyway, and translates back when updating the model.
As a result, the HTML will look incorrect but the model will still be set properly when choosing a value. (i.e. AngularJS will translate '0' back to 'var1')
The solution by Epokk also works, however if you're loading data asynchronously you might find it doesn't always update correctly. Using ngOptions will correctly refresh when the scope changes.
You can use ng-repeat with option like this:
<form>
<select ng-model="yourSelect"
ng-options="option as option for option in ['var1', 'var2', 'var3']"
ng-init="yourSelect='var1'"></select>
<input type="hidden" name="yourSelect" value="{{yourSelect}}" />
</form>
When you submit your form you can get value of input hidden.
DEMO
ng-selected
ng-repeat
If you setup your select like the following:
<select ng-model="myselect" ng-options="b for b in options track by b"></select>
you will get:
<option value="var1">var1</option>
<option value="var2">var2</option>
<option value="var3">var3</option>
working fiddle: http://jsfiddle.net/x8kCZ/15/
you could use something like
<select ng-model="myselect">
<option ng-repeat="o in options" ng-selected="{{o==myselect}}" value="{{o}}">
{{o}}
</option>
</select>
using ng-selected you preselect the option in case myselect was prefilled.
I prefer this method over ng-options anyway, as ng-options only works with arrays. ng-repeat also works with json-like objects.
<select ng-model="option" ng-options="o for o in options">
$scope.option will be equal to 'var1' after change, even you see value="0" in generated html
plunker
Hay all,
I am trying to validate a select in kohana 3.0 and I am using the necessary rules. However the validation does no "kick in" when the user does not make a selection.
<select id="discipline" name="discipline" >
<option value="0"> -- Select One -- </option>
<option value="-2">Information Technology and Engineering</option>
<option value="4">Business and Training Seminars</option>
</select>
That was my select, now i have applied these rules to the post array before i check for validation errors.
$post = Validate::factory($_POST)
->rule('discipline', 'not_empty')
->rule('discipline', 'numeric');
When I submit the form without making a selection, the form submits and the rules should stop it.
Any ideas ?
Your still putting a value for the first one, as 0. Leave the value as value="". 0 is numeric and considered not empty.
Replace 0 with blank string
Add ->rule('discipline', 'in_array', array(array(-2, 4))); to check that selected discipline is within valid collection.
string empty first option
<select name="fruit" required>
<option value="">select fruit</option>
<option value="banana">banana</option>
</select>
then some css may help
form:invalid button[type="submit"], form:invalid input[type="submit"] {
opacity: 0.5;
cursor: default;
}