Select an option onto a select box using python

3,316 views
Skip to first unread message

Thomas CARPENTIER

unread,
Aug 4, 2011, 11:40:19 AM8/4/11
to seleniu...@googlegroups.com
Hi!

I've tried to select an option onto a listbox but it raise an error. 

This is my code : 

selectBox = self.driver.find_element_by_xpath(select_pension_locators)
options = selectBox.find_elements(by=By.TAG_NAME, value="option")
         myoption = options[1]
                   myoption.select()


After running this code, i get : 

E       AttributeError: 'WebElement' object has no attribute 'select'

if someone can help me !!!

Thanks!

Thomas


Mark Collin

unread,
Aug 4, 2011, 11:53:38 AM8/4/11
to seleniu...@googlegroups.com

Use the Select (or SelectElement in .Net) object  e.g.:

 

WebElement foo = driver.findElement(By.xpath("//select"));

Select se = new Select(foo);

se.selectByVisibleText("<textOfOptionToSelect>");

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/rTZofUEcMgMJ.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


-- This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error please notify postm...@ardescosolutions.com

Thomas CARPENTIER

unread,
Aug 4, 2011, 11:56:23 AM8/4/11
to seleniu...@googlegroups.com
I've just read this from the python bindings change logs  :

Selenium 2
* Removed toggle() and select()

Selenium 2 RC 3
* Added Opera to Desired Capabilities
* Removed deprecrated methods
* Deprecated toggle() and select() methods. This will be removed in the next release

Thomas CARPENTIER

unread,
Aug 4, 2011, 12:22:55 PM8/4/11
to seleniu...@googlegroups.com
I found a solution : 

select_by_value(self.driver.find_element_by_xpath(select_pension_locators), "Thomas")

def select_by_value(self, webElement, value):
        '''
        This function selects an item given by
        the argument 'value' within the 'WebElement' object
        '''
        options = webElement.find_elements_by_tag_name("option")
        for option in options:
            if option.text == value:
                webElement.click()
                webElement.send_keys(option.text, Keys.RETURN)

Saran Saravanan

unread,
Jun 3, 2014, 5:39:35 AM6/3/14
to seleniu...@googlegroups.com
obj.selectByVisibleText()
is a function in javascript ! but what is the equivalent function in python ??

Doug Dragon

unread,
Jun 3, 2014, 1:15:08 PM6/3/14
to seleniu...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages