Drop down list select using python selenium (xpath) - python-2.7

Month
<select name="filterMonth" onchange="document.form.submit()">
<option value="0"></option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
How can I select drop down list for "filterMonth" using xpath?
browser.find_element_by_xpath('//*[#id="form"]/table[1]/tbody/tr[3]/td[2]/select[2]')
gives me error that no such element found

You can use the xpath
//select[#name='filterMonth']
if name is unique across the page.
Select select =new Select(driver.findelement(By.xpath("//select[#name='filterMonth']")));
select.selectByVisibleText("January");
This for Java. please change it accordingly for python.

You can try following code:
select = Select(driver.find_element_by_xpath("//select[#name='filterMonth']"))
# select by visible text
select.select_by_visible_text('January')

One of the reasons why you get NoSuchElementException might be using tbody tag in your XPath as this tag not always present in initial HTML source, but might be generated dynamically. The other reason is that entire table could be generated dynamically and you should wait a little to be able to handle it:
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
select = wait(browser, 10).until(EC.presence_of_element_located((By.NAME, "filterMonth")))
Select(select).select_by_visible_text("March")
Also you can use provided by #Murthi //select[#name="filterMonth"] XPath to select drop-down

Related

ng-change isn't working for select.(selecting option) (AngularJs)

<select class="map-columns" ng-model="col" ng-change="changeValueInExcelData(false, $parent, $index, col)">
<option value="power_market">Power market</option>
<option value="customer">Customer</option>
</select>
Here i have select with option, Now when i change my option ng-change function isn't calling.
I used ng-change with input it worked successfully but I don't know why it isn't working for selection.

Django dropdown select by text

I have a simple django dropdown form. I want to grab the text instead of the value.
<select name="doctors" required="" id="id_doctors">
<option value="" selected="">---------</option>
<option value="3">sometext</option>
</select>
On selection I want to use the some text in the backend.
if 'bookAppointment' in request.POST:
print(request.POST['doctors'])
print(int(request.POST['doctors'])-1)
print(Profile.objects.filter())
this prints out the value 3 is there a way to just get some text out.
3
2
<QuerySet [<Profile: some>, <Profile: some>, <Profile: some text>]>
I set it up like so:
bookAppointment = BookAppointmentForm()
# Make sure I get active doctors and doctors who have a refresh_token
bookAppointment.fields['doctors'].queryset = Profile.objects.filter(Q(is_active=True)&Q(is_doctor=True))
context['bookAppointment'] = bookAppointment
Just change the value to your text, that way the text is passed through to POST instead of the value.
Otherwise you will need to add in some controls:
if self.request.POST['doctors'] == 3:
my_var = "sometext"

Selecting pull down item with python selenium

I've been trying to select an item from a pull down list. I've been searching and trying everything. I've tried to search and select by id, xpath, and name. I am at a complete loss. I keep seeing select_by_value but I get the error 'WebElement' object has no attribute 'select_by_value'
The code from the website that I'm trying to select from is as follows.
<input type="hidden" name="wlw-select_key:{actionForm.dobMonth}OldValue" value="true">
<select name="wlw-select_key:{actionForm.dobMonth}" id="dobMonth" class="dobMonth"> <option value="">Select month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option></select>
I've tried both working with the numerical value and the text value, but neither have worked. I've also tried selecting with the id and then sending keyboard commands. The best that I can get it to do is to open the menu, but it does not change the value.
driver.find_element_by_id('dobMonthSelectBoxItContainer').send_keys("May")
doesn't give errors, but when I find by xpath or name I do get errors. I've been using firepath to get the xpath, which would be
.//*[#id='genderSelectBoxItText']
and
.//*[#id='5']/a
(I've gotten to the 5th page on several google searches :/ Most I see using the select command, but I keep getting that error.)
It's best to use the Select() class when handling a <select> element.
from selenium.webdriver.support.select import Select
select = Select(driver.find_element_by_id("dobMonth"))
select.select_by_visible_text("May")
This is how I handle dropdowns with Selenium
def select_dropdown(driver, dropdown_id, option_value):
dd = driver.find_element_by_id(dropdown_id)
xpath = ".//option[#value='{}']".format(option_value)
dd.find_element_by_xpath(xpath).click()
In your case, if you were trying to select May, as in your example code, it would be select_dropdown(driver, 'dobMonth','05').
I did find a solution, but it is bulky and seems to take more time than necessary. The solution I have created is the following code.
driver.find_element_by_id('dobMonthSelectBoxItText').click()
time.sleep(1) #requires "import time"
driver.find_element_by_id('dobMonthSelectBoxItText').click()
driver.find_element_by_id('5').click()
This seems to open the box twice, but if I remove any line it does not work. I feel that this is bad coding technique, but this is a working solution. I would still appreciate a more elegant solution than this, if anyone finds one.

Insert a dropdownlist inside an article in joomla

Thanks and regards.Hello, I'm trying to put a dropdownlist inside an article but I don't know how, could you help me?, any advice. I use joomla 2.5
All you have to do is insert in your article the HTML code of a drop down list.
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
Go to your article and in the editor you will find a button which is a white page with these symbols: <> (Source Code Editor).
Once you click on this you will see your article in HTML format and you can insert here the code given above and edit it your own way.
Hope this helps.

Play framework select field empty value

How do I add a "none" option to the play framework select field?
So far, I have this:
<select size="1" name="object.id">
<option value="">&{'crud.none'}</option>
#{list items:someItems, as:'item'}
<option value="${item.id}">${item.name}</option>
#{/list}
</select>
but when i select the "none" value, play constructs a new object, and tries to save the parent object with a reference to the newly created object, resulting in a hibernate org.hibernate.TransientObjectException
Any ideas?
Set none option's value to 0 and in your controller add relation only in case if (item.id > 0)
<option value="0">&{'crud.none'}</option>
What's more if this value is required you can use simple check with JavaScript to ensure, that user selected some option