how to select a option on a web page drop down list using selenium with python - python-3.x

I wanted to select the value "LK" in the drop down list on my web page. please help.
i tried to slect by xpath as follows
driver.find_element_by_xpath("//select[contains(text(), 'GLOBAL')]").click()
driver.find_element_by_xpath("//span[contains(text(), 'LK')]/..").click()
but i get following error:
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
Below is the web element code im trying to fetch:
<select style="border-radius: 3px;" ng-model="selectedRcc.value" ng-options="item for item in ['LK', 'US', 'GLOBAL']" class="ng-pristine ng-valid ng-touched" tabindex="0" aria-invalid="false">
<option value="0" label="LK">LK</option>
<option value="1" label="US">US</option>
<option value="2" selected="selected" label="GLOBAL">GLOBAL</option>
</select>

Use the below xpath
//select[#class='ng-pristine ng-valid ng-touched']/option[.='LK']
Code:
driver.find_element_by_xpath("//select[#class='ng-pristine ng-valid ng-touched']/option[.='LK']").click()

When you have select element it always good to use selenium select class to select item from drop down menu.
select=Select(driver.find_element_by_xpath("//select[#class='ng-pristine ng-valid ng-touched']"))
select.select_by_visible_text('GLOBAL')
select.select_by_visible_text('LK')
OR you can use index.
select=Select(driver.find_element_by_xpath("//select[#class='ng-pristine ng-valid ng-touched']"))
select.select_by_index(0) #LK
select.select_by_index(2) #GLOBAL
To use above code you need to have following imports.
from selenium.webdriver.support.select import Select

Related

ng-selected does not work but modifies option as selected="selected", working with ng-repeat

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

Select a dropdown using Python + Selenium

I am writing an automation for work and am stuck with a dropdown. The particular select box in question is as follows:
<span class="a-dropdown-container" id="select-size-bulk-asin">
<select name="display_type" class="a-native-dropdown">
<option value="SMALL-IMAGES">SMALL-IMAGES</option>
<option value="LARGE-IMAGES">LARGE-IMAGES</option>
<option value="TEXT">TEXT</option>
</select>
<span tabindex="-1" data-a-class="a-spacing-small" class="a-button a-button-dropdown a-spacing-small">
<span class="a-button-inner">
<span class="a-button-text a-declarative" data-action="a-dropdown-button" aria-haspopup="true" role="button" tabindex="0" aria-pressed="false" aria-owns="2_dropdown_combobox">
<span class="a-dropdown-prompt">SMALL-IMAGES</span>
</span>
<i class="a-icon a-icon-dropdown"></i>
</span>
</span>
</span>
It defaults to 'SMALL Images' and I would like to select the 'TEXT' option. I am receiving element not clickable error. The page is simple and the element is visible on the screen.
The list of methods I did try are:
Used WebDriverWait to wait for the element to be visible;
Used WebDriverWait to wait for the element to be clickable;
Used the select class to set the selected option;
I also read through a question.
I am thinking if I should just go to the next element and send Shift+Tabs until I reach this drop down and then down arrow keys. But would like to use that only as the last resort.
NOTE:
- I am using Python 3 and Chrome.
You can try this code to select value from drop down :
select = Select(driver.find_element_by_id('select-size-bulk-asin'))
select.select_by_visible_text('TEXT')
However,as you have mentioned you are receiving element not clickable exception. you can try this code :
WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.ID, "select-size-bulk-asin")))
As a last resort you can go ahead with :
drop_down= driver.find_element_by_id("select-size-bulk-asin")
drop_down.click()
actions = ActionChains(driver)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ENTER)
actions.perform()

How to select a value from a drop-down using Selenium and Python

I want to select an option from a select field using Selenium & Python.
The HTML is as follows:
<select autocomplete="off" class="style_input_item" name="AccountEnable" id="Enable" value="0" onchange="onPageDataChange()">
<option value="0" selected="selected"><script>T("Disabled")</script>Disabled</option>
<option value="1"><script>T("Enabled")</script>Enabled</option>
</select>
And I tried as follows:
driver.find_element_by_xpath('//*[#id="Enable"]/option[value="1"]').click()
I received that error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="Enable"]/option[value="0"]"}
Just try:
mydriver.find_element_by_xpath('//*[#id="Enable"]/option[#value="1"]').click()
or
mydriver.find_element_by_xpath('//*[#id="Enable"]/option[2]').click()
Make sure you include Select:
from selenium.webdriver.support.select import Select
then
select = Select(driver.find_element_by_id('Enable'))
select.select_by_index(0)

Group 'input option values' in template variable with 'listbox' type

I would like to apply the grouped text in listbox, looks like this
to
this is listbox in the resources page.
I tried to add the ‘optgroup’ tag into
'manager\templates\default\element\tv\renders\input\listbox-multiple.tpl'
but still not working, the 'optgroup’ tag will be ignored.
I unterstand that I should ‘Adding a Custom TV Input Type’ but the document didn’t include the details about grouping input values.
That is easy ;)
your listbox should contain a #CHUNK yourchunk
In that chunk you can perform nearly everything, that genereates a list, like:
<select name="something" id="something" >
<option value="" selected disabled>choose</option>
<optgroup label="Headline 01">
[[pdoResources? &parents=`0` &where=`{"template:=":40` &sortby=`{"value":"ASC"}` &limit=`40` &tpl=`pdoResourcesFormoption-something` &includeTVs=`some-tv`]]
</optgroup>
<optgroup label="Headline 02">
[[pdoResources? &parents=`0` &where=`{"template:=":41` &sortby=`{"value":"ASC"}` &limit=`40` &tpl=`pdoResourcesFormoption-something` &includeTVs=`some-tv`]]
</optgroup>
<optgroup label="Headline 03">
[[pdoResources? &parents=`0` &where=`{"pagetitle:=":"something"` &sortby=`{"value":"ASC"}` &limit=`40` &tpl=`pdoResourcesFormoption-something` &includeTVs=`some-tv`]]
</optgroup>

can we add textbox and button to select 2

here i attached the code of select can we add the textbox and button into select2 elements.
here which i used a code:
<select style="width:100%" id="e9">
<option value="volvo">Suger</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
here i attached the output image how i really want:
here is the answer...
$(".select2-drop").append('<div><input type="text" style="width: 86%;padding: 9px;"name="lname"><input class="PrimaryBtn" type="submit" value="Add"></div>');
$("#e9").select2("container").find("div.select2-drop").append('<div><input type="text" name="lname"><input class="PrimaryBtn" type="submit" value="Add"></div>');
may be this will help, it will add to the needed select2 only

Resources