How to select a dropdown with optgroup in Selenium? - python

<select class="ms-crm-SelectBox" sort="ascending" defaultselected="-1" id="advFindE_fieldListFLDCTL" style="" xpath="1">
<optgroup id="fld" label="Fields">
<option value="xyz_accountnumber" datatype="nvarchar" maxlength="100" title="Account Number">Account Number</option>
<option value="xyz_addsource" datatype="nvarchar" maxlength="100" title="Add Source">Add Source</option>
<option value="xyz_addsourceleadid" datatype="nvarchar" maxlength="100" title="Add Source Lead ID">Add Source Lead ID</option>
</optgroup>
</select>
I have trying to select Account Number,I am using python
dropdown_list = Select(self.driver.find_element_by_css_selector('#advFindE_fieldListFLDCTL'))
dropdown_list.select_by_value('xyz_accountnumber')
I am getting the below error
selenium.common.exceptions.ElementNotInteractableException: Message:
element not interactable: Element is not currently visible and may not
be m.
Can you help?

This seems to work for me
element = driver.find_element_by_class_name('ms-crm-SelectBox')
element.click()
option = driver.find_element_by_xpath("//option[#value='xyz_addsource']")
option.click()
Is this what you are trying to do?

Related

how to get list of all available values in select (HTML) using flask

I have a piece of html code where i use select and have a list of options the user can choose from.
<td>
<select id="target" name="target">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
My python code:
listOfAllOptions = request.form.getlist('target')
The above code only gives the selected option. However, I want list of all the options available in the select.
Expected output: ["1","2","3"]
Please help.
Try the lxml library:
$ pip install lxml
And the code:
from lxml import html
html_string = """<td>
<select id="target" name="target">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
"""
dom_tree = html.fromstring(html_string)
options_lst = dom_tree.xpath("//option//text()")
print(options_lst)
output: ['1', '2', '3']

Selecting an item from a list with Selenium and Python

I'm trying to select an item from a list with selenium / python and am having issues with getting the elements to load and be interactable. I think the problem is the website doesn't load the html for that section until the drop down is clicked but I'm not sure how to go about resolving that. This is what the html looks like if I click view source:
<label data-lang="4003">Impact</label>
<select id="impact">
<option value="" selected data-lang="48">Select</option>
</select>
If I look at it with the chrome inspect tools I see this:
<label data-lang="4003">Impact</label>
<div class="select-wrapper cascading-dropdown-loading initialized">
<span class="caret">▼</span>
<input type="text" class="select-dropdown" readonly="true" data-activates="select-options-e89d2a9c-3be1-67a1-6f4b-32c36b38f6eb" value="Select">
<ul id="select-options-e89d2a9c-3be1-67a1-6f4b-32c36b38f6eb" class="dropdown-content select-dropdown ">
<li class="">
<span>Select</span>
</li>
<li class="">
<span>High</span>
</li>
<li class="">
<span>Medium</span>
</li>
<li class="">
<span>Low</span>
</li>
<li class="">
<span>None</span>
</li>
</ul>
<select id="impact" class="initialized" data-select-id="e89d2a9c-3be1-67a1-6f4b-32c36b38f6eb">
<option value selected data-lang="48">Select</option>
<option value="HIGH">High</option>
<option value="MEDIUM">Medium</option>
<option value="LOW">Low</option>
<option value="NONE">None</option>
</select>
</div>
The data-select-id also seems to be randomly generated, as it changes each time I refresh the page.
So far, I've been able to get a list of the options available using the following code:
select = driver.find_element_by_id("impact")
options = [x for x in select.find_elements_by_tag_name("option")]
for element in options:
print(element.get_attribute("value"))
Attempting to manipulate the list with something like element.click() doesn't seem to work and only gives me the error "Error: Message: element not interactable: Element is not currently visible and may not be manipulated"
I have also tried using Select with code like this with no success. I've used all three select by options, select_by_value, select_by_visible_test and select_by_index with no success.
select = Select(driver.find_element_by_id('impact'))
select = select_by_value("HIGH")
I have also tried exploring select.options which returns a list of objects that look like:
<selenium.webdriver.remote.webelement.WebElement (session="513ab2b2bfc1c3c738af19e69230a763", element="d00bf83d-3772-4357-b6da-1702a12ffda3")>
but I'm not sure how to manipulate these to do anything more with them. The session and element ids also change between runs.
Any help pointing me in the right direction would be greatly appreciated!

Element Not Interactable Exception when using Python selenium Select() function

I have the following HTML code:
<select name="course" id="course" class="standardSelect form-control-sm form-control" data-live-search="true" data-size="10" onchange="displayStudent(this.value);" style="display: none;">
<option value="">Select course</option>
<option value="2">Course A</option>
<option value="71">Course B</option>
<option value="5">Course C</option>
...
</select>
<div class="chosen-container chosen-container-single chosen-container-single-nosearch" title="" id="course_chosen" style="width: 100%;">
<a class="chosen-single">
<span>Select course</span>
</a>
<div class="chosen-drop">
<div class="chosen-search">
<input class="chosen-search-input" type="text" autocomplete="off" readonly="">
</div>
<ul class="chosen-results">
<li class="active-result result-selected" data-option-array-index="0">Select course</li>
<li class="active-result" data-option-array-index="1">Course A</li>
<li class="active-result" data-option-array-index="2">Course B</li>
<li class="active-result" data-option-array-index="3">Course C</li>
...
</ul>
</div>
</div>
I am intending to scrape the content that is displayed when each course is selected. So I have to click on each option and get the page's source code. However I am unable to do so. When I try to select any option either using the select_by_value() or using the select_by_visible_text() functions I get the ElementNotInteractableException. I also tried the select_by_index() function.
Below is the code I used for selecting the option with value = 2:
select_box = Select(browser.find_element_by_xpath("//select[#id='course']"))
select_box.select_by_value('2')
and the error it gives me is:
ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
(Session info: chrome=80.0.3987.149)
Please help me resolve this exception or any other method that would work on selecting the option from a drop down list.
the select is not displayed style="display: none;" so you cannot click it
I guess on the real side you would have to click on the li element. try to click: //div[#id="course_chosen"]//ul/li[text()="Course A"] (probably after clicking on <span>Select course</span>)

List selected options in Selenium Python

I'm trying to list the selected options from the page. But nothing is printed with below code. Any help will be appreciated.
HTML code:
<select class="chosen-select" id="tag_opts" name="tag_opts[]" tabindex="-1" multiple="true" data-placeholder="Select a call tag here" disabled="disabled">
<optgroup label="TAGS">
<option value="1" selected="selected">1</option>
<option value="2" selected="selected">2</option>
<option value="3" selected="selected">3</option>
<option value="4" selected="selected">4</option>
</optgroup>
</select>
What I tried:
el = driver.find_element_by_id("tag_opts")
for option in el.find_elements_by_tag_name('option'):
if option.text in labels:
print(option)
I tried with many other options but couldn't succeed.
Use Select to handle <select> elements :
select = Select(driver.find_element_by_id("tag_opts"))
options = select.options
for option in options:
print(option.text) #or print(option.get_attribute("value"))
Just comment out the 'if' statement and print(option.text)
el = driver.find_element_by_id("tag_opts")
for option in el.find_elements_by_tag_name('option'):
#if option.text in labels:
print(option.text)
PS your if statement refers to a 'labels' object that isn't defined. So it will never match anything

webdriver.support.select - Select doesn't interact with the element

Hello,
I have the following select:
<div class="col-xs-12 col-md-6">
<label class="ph_label pointer city">
<span class="l citylbl">City</span>
<select class="cities custom-dropdown" data-validation-error-msg="You must select a city" required="" name="city" disabled="disabled" data-validation="required">
<option value="">City</option>
<option value="Shanghai">Shanghai</option>
<option value="Beijing">Beijing</option>
<ul class="cities custom-dropdown" data-target-selector="select.cities" style="width: 293px;">
</label>
</div>
I'm trying:
mySelect = Select(self.driver.find_element_by_name("city"))
mySelect.select_by_value("Shanghai")
When I see the test run, selenium goes through this element as if everything was OK, but in fact the city was not selected.
Any ideia?
I think this is due to the select in the code snippet is marked as disabled?
I have tried using Watir-webdriver, and if the disabled="disabled" attribute is there, I get the behavior you describe. As soon as I remove it, the value I want is selected.

Categories