Tuesday 18 March 2014

Selecting value from the combobox in selenium webdriver in python - Example

To work with drop down elements you will need to import below class.
from selenium.webdriver.support.ui import Select

We can select the value from the dropdown using 3 different methods.
1.      select_by_visible_text – using the actual text displayed in drop down.
2.      select_by_value –using the value of the option
3.      select_by_index – using position of the item

Below example demonstrates how we can select the value from the drop down using select_by_visible_text method.

from selenium import webdriver

from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()

driver.get("http://register.rediff.com/register/register.php")

driver.maximize_window()

try:
                select=Select(driver.find_element_by_name('country'))
    select.select_by_visible_text("China")

driver.close()

except Exception as e:

    print ("Exception occured", format(e));

finally:

    driver.quit()

    print ("finally")



What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors