List selected options in Selenium Python - 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

Related

How to print the text of the preselected option in a dropdown menu with Selenium using Python?

I'm writing a python script to grab a preselected option in a dropdown menu and then storing that grabbed option in a variable that is created. Need some help with getting the preselected option.
As you can see below, the option with an already existing "selected"
When the page loads, the text box is already filled with the text relevant to that option tag
<select name="ctl00$ContentPlaceHolder1$homeParkDropDownList" id="ContentPlaceHolder1_homeParkDropDownList">
<option value="-1"></option>
<option value="2"></option>
<option value="3"></option>
<option value="15"></option>
<option selected="selected" value="8"></option>
<option value="9"></option>
<option value="12"></option>
<option value="100"></option>
<option value="19"></option>
<option value="14"</option>
<option value="13"></option>
<option value="18"></option>
<option value="6"></option>
<option value="4"></option>
<option value="5"></option>
<option value="1"></option>
<option value="7"></option>
<option value="34"></option>
<option value="11"></option>
</select>
I would like to grab the text that already has that 'selected' tag
Current code so far
select = Select(driver.find_element(By.ID, "ContentPlaceHolder1_homeParkDropDownList"))
options = select.options
You need to use first_selected_option as follows:
select = Select(driver.find_element(By.ID, "ContentPlaceHolder1_homeParkDropDownList"))
element = select.first_selected_option
To print the text:
print(Select(driver.find_element(By.ID, "ContentPlaceHolder1_homeParkDropDownList")).first_selected_option.text)

How to select a option value, click in another menu and then hit submit

I have this page with two menus and a submit button.
I would like to select an option in the first menu (companies), then select an item in the second menu (type) and finally hit the submit button (Send)
Here is the simplified HTML page:
<select name="companies" multiple="multiple" id="IDcompanies" style="width:200px;">
<option value="01">Facebook</option>
<option value="02">Oracle </option>
<option value="03">AWS</option>
<option value="04">Tesla</option>
</select>
<select name="type" id="IDtype" style="width:200px;">
<option value="T1">Type1 </option>
<option value="T2">Type2 </option>
<option value="T3">Type3</option>
</select>
<input type="submit" name="Button1" value="Send" id="ID_Button1" />
In python, I'm trying this to the first part: click on the first company (Facebook):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get(url)
box = driver.find_element(By.ID, "IDcompanies")
box.select_by_index(0)
But gives me an error: AttributeError: 'WebElement' object has no attribute 'select_by_index'
I appreciate if someone can help with this error and guide me on how to proceed to make this sequence of clicking on the first menu, then on the second and then on the submit button.
In order to use special Selenium methods like select_by_index, select_by_value and select_by_visible_text you should define and initialize the special Selenium Select object, as following:
companies_select = Select(driver.find_element(By.ID, "IDcompanies"))
companies_select.select_by_index(0)
For more details see here

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']

click on element in dropdown option menu - Selenium Python

I want to click on one element from a dropdown menu which has the following html code:
<select style="font-size:10px" onchange="dg_send('contractNonParticipationsDatagrid1-form', 'contractNonParticipationsDatagrid1', 'search', '/masterdata/datagridContractNonParticipations/dg_page/1/tabId/non-participations/id/1/licenseHolderId/1/dg_sort/dvec.full_name/dg_order/asc/dg_rowlimit/' + this.options[this.selectedIndex].value,false); "><option selected="selected" value="10">10</option><option value="25">25</option><option value="50">50</option><option value="100">100</option><option value="999999999">alle</option></select>
<option selected="selected" value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="999999999">all</option>
I would like to select the last one, so that all are displayed.
Thank you!
To get the last option of that tag try and get it's xpath value and do the following.
from selenium.webdriver.support.select import Select
sel = Select(driver.find_element_by_xpath("//select[#style='font-size:10px']"))
sel.select_by_visible_text("all")
You could also try this
from selenium import webdriver
from shutil import which
path = which("chromedriver")
driver = webdriver.Chrome(executable_path=path)
all_btn = driver.find_element_by_xpath("//select/option[contains(text(),'all'))
all_btn.click()

How to select a dropdown with optgroup in Selenium?

<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?

Categories