Selenium selecting from Dropdown Python - python

I'm using selenium in python and I'm looking to select the option Male from the below:
<div class="formelementcontent">
<select aria-disabled="false" class="Width150" id="ctl00_Gender" name="ctl00$Gender" onchange="javascript: return doSearch();" style="display: none;">
<option selected="selected" title="" value="">
</option>
<option title="Male" value="MALE">
Male
</option>
<option title="Female" value="FEM">
Female
</option>
</select>
Before selecting from the dropdown, I need to switch to iframe
driver.switch_to.frame(iframe)
I've tried many options and searched extensively. This gets me most of the way.
driver.find_element_by_id("ctl00_Gender-button").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "ctl00_Gender")))
select=Select(driver.find_element_by_id("ctl00_Gender"))
check=select.select_by_visible_text('Male')
If I use WebDriverWait it times out.
I've tried selecting by visible text and index, both give:
ElementNotInteractableException: Element could not be scrolled into view

As per the HTML you have shared the <select> tag is having the value of style attribute set as display: none;. So using Selenium it would be tough interacting with this WebElement.
If you access the DOM Tree of the webpage through google-chrome-devtools, presumably you will find a couple of <li> nodes equivalent to the <option> nodes within an <ul> node. You may be required to interact with those.
You can find find a couple of relevant detailed discussions in:
select kendo dropdown using selenium python
How to test non-standard drop down lists through a crawler using Selenium and Python
How to select an option from a dropdown of non select tag?

Try below solutions:
Solution 1:
select = Select(driver.find_element_by_id('ctl00_Gender'))
select.select_by_value('MALE')
Note add below imports to your solution :
from selenium.webdriver.support.ui import Select
Solution 2:
driver.find_element_by_xpath("//select[#id='ctl00_Gender']/option[text()='Male']").click()

Related

Python Selenium webdriver get XPATH and select dropdown

I've found the word 'Burger' in HTML table with this code
findRow = driver.find_element(By.XPATH, "//*[contains(text(),'Burger')]").value_of_css_property('#name')
how do I can get XPATH 'Burger'?
how do I can select the column beside it (example select 'Fish' beside column 'Burger') and then submit button?
HTML code
<tbody>
<tr>
<td>..</td>
<td>.....</td>
</tr>
<tr>
<td>Burger</td>
<td>
<select class="form-control input-sm" id="sel222" name="sel222" type="68" group="433" onchange="count(this)">
<option value="1">Vegetables</option>
<option value="2">Fish</option>
<option value="3">Beef</option>
</select>
</td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-primary" id="submit"><span class="fa fa-save"></span> Save</button>
Based on the HTML posted, the simplest XPath to find "Burger" would be,
//td[text()='Burger']
The XPath to find the SELECT in the cell to the right of the "Burger" cell would be,
//td[text()='Burger']/following-sibling::td/select
^ the XPath to find the TD that contains "Burger", from above
^ then find the sibling TD
^ then the SELECT child of the sibling
Putting this all together to select "Fish" from the SELECT element next to the "Burger" cell and click Submit,
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element(By.XPATH, "//td[text()='Burger']/following-sibling::td/select"))
select.select_by_visible_text("Fish")
driver.find_element(By.ID, "submit").click()
In this scenario, you can identify the select list box using xpath following technique. Use the below xpath to identify select object
//td[contains(.,'Burger')]/following::select
You can select the option Burger using the below XPath:
.//td[text()='Burger']
You can select the options inside select tag using the below XPath:
.//td[text()='Burger']//parent::tr//select/option

Python / Selenium - access not interactive element

What I need:
Select one of the options from the drop-down list
Only BUTTON element is visible (SELECT element is not visible)
This is html code:
<td data-bind="css: { 'cell-error': applicantNames.hasError }, attr: { title: applicantNames.message }" title="">
<select data-bind="options: $parent.applicants, multivalue: applicantNames" multiple="multiple" style="display: none;">
<option value="ABC">ABC</option>
<option value="XYZ">XYZ</option>
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 236px;"><span class="ui-icon ui-icon-triangle-2-n-s"></span><span>Please select...</span></button>
</td>
This is how it looks like:
The problem is that I would like to select one of these options by using Selenium, but because the SELECT element is not interactable/not visible ... not sure what to do...
If you will choose an option from the drop-down the BUTTON element will be updated like this
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all ui-state-active" aria-haspopup="true" style="width: 236px;">
<span class="ui-icon ui-icon-triangle-2-n-s"></span><span>ABC, XYZ</span>
</button>
Even if I will update the BUTTON section manually, it will still not update the element visually..... so I'm a bit stuck to find a way how to interact with these elements by using Selenium...
P.S. I'm looking for ANY headless methods, e.g. Selenium / JavaScript execution through selenium and e.t.c...
See the problem is that "This is not built using Select - option tag", so one can not directly make use of Select class from Selenium support package.
Instead you can try to click on that menu using Selenium .click()
in your case something like this
driver.find_element_by_xpath("//span[contains(text(), 'Please select')]").click()
and then store all the visible option in a list of web elements using find_elements and then iterate the list :
for option in list_of_options:
if option.text == "your desired option"
option.click()

UnexpectedTagNameException: Select only works on <select> elements, not on "<form>" error selecting a drop-down value using Selenium and Python

I am trying to select an option from this dropdown with Selenium using Python
<div class="form-group mt-2 mb-3 p-3">
<form id="did_atd_provide_pnc">
<fieldset>
<label for="did_atd_provide_pnc">Did ATD Consultants provide a Plan & Cost review for
this project*?</label>
<select class="form-control form-control-sm required-field atd_provided_dropdown" id="did_atd_provide_pnc"
name="did_atd_provide_pnc" data-error-name="Did ATD Provide P&C">
<option value="">-----</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</fieldset>
</form>
</div>
I am writing this code
select = Select(driver.find_element_by_id('did_atd_provide_pnc'))
select.select_by_visible_text('No')
But I am getting this error
UnexpectedTagNameException: Select only works on <select> elements, not on "<form>"
Is there any other solution I can test this case with any other reliable solution?
You were close enough. However the first element to be identified through find_element_by_id('did_atd_provide_pnc') is the <form> tag, where as your desired element is the <select> tag.
Hence, the Select() throws error with the <form> element as:
UnexpectedTagNameException: Select only works on <select> elements, not on "<form>"
Solution
To select <option> with text as No you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following xpath based Locator Strategies:
Using xpath and select_by_visible_text():
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[#id='did_atd_provide_pnc' and #name='did_atd_provide_pnc']")))).select_by_visible_text("No")
Using xpath select_by_value():
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[#id='did_atd_provide_pnc' and #name='did_atd_provide_pnc']")))).select_by_value("No")
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
References
You can find a couple of relevant detailed discussions in:
UnexpectedTagNameException: Message: Select only works on elements, not on error selecting an Dropdown option using Selenium and Python

How to select a drop-down menu value wihtout an id with Selenium using Python?

I need to select an element/item from a drop-down menu that doesn't have an id element using Python and Selenium.
The piece of HTML code:
<mbo-transaction-mode-select mode="filters.mode" class="ng-isolate-scope">
<select class="form-control input-sm ng-pristine ng-untouched ng-valid ng-empty" ng-model="mode" ng-options="value for (key,value) in vm.modes">
<option value="" class="" selected="selected"></option>
<option label="LIVE" value="string:LIVE">LIVE</option>
<option label="TEST" value="string:TEST">TEST</option>
</select>
The current option I found on Stackoverflow or Google used the Select method, but that option used find_element_by_id which I unfortunately don't have.
I tried to use:
select = Select(browser.find_element_by_xpath("//input[#ng-model='mode']"))
select.select_by_visible_text('LIVE')
But this gave the error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[#ng-model='mode']
Is there another way for me the select the dropdown and one of its options?
You need to fix your xpath, as here:
element = browser.find_element_by_xpath("//select[#ng-model='mode']")
driver.execute_script("arguments[0].scrollIntoView();", element)
select = Select(element)
select.select_by_visible_text('LIVE')

Unable to select value from drop down with python selenium webdriver even when elements are visible

html code
</p>
<p>
<label>Capacity:</label>
<select name="capacity">
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
</select>
</p>
<p>
This is what I have tried so far
policy_mgmt = Select(mydriver.find_element_by_xpath("//select[#name='Capacity']"))
policy_mgmt.select_by_value("Medium")
Does not throw any error but Medium does not get selected
I also tried below code
mydriver.find_element_by_xpath(".//select[#name='capacity']").click()
mydriver.find_element_by_xpath("//select[#name='capacity'] /option[#value='Medium']").click()
This clicks on the drop down and I can see all the options on the webpage, but it does not select "Medium" as I expect.
Any help will be highly appreciated
xpath is case sensitive, it should be capacity with lowercase 'c'
policy_mgmt = Select(mydriver.find_element_by_xpath("//select[#name='capacity']"))
browser.find_element_by_xpath(".//select[#name='capacity']/option[text()='Medium']").click()

Categories

Resources