I am writing tests with selenium webdriver 2.4 on python 2.7.
The documentation (http://seleniumhq.org/docs/03_webdriver.html) demonstrates the ability to manipulate select form elements as follows:
Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText("Edam");
I need to manipulate select form elements like this but in python. However I cannot figure out what to import to successfully instantiate the Select object.
What should my import statement be?
T开发者_如何学Chanks.
However I cannot figure out what to import to successfully instantiate the Select object.
It is available as:
from selenium.webdriver.support.ui import Select
See also: http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_support/selenium.webdriver.support.select.html#module-selenium.webdriver.support.select
Since I posted this question I've spent a good amount of time searching for the python equivalent to the java Select() object and found nothing.
I've come up with a work around based on this: https://gist.github.com/1205069
Perhaps the following code will help someone save some time.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
def select_by_text(web_element, select_text):
"""given a web element representing a select object, click the option
matching select_text
"""
option_is_found = False
options = web_element.find_elements_by_tag_name('option')
for option in options:
if option.text.strip() == select_text:
option.click()
option_is_found = True
break
if option_is_found == False:
raise NoSuchElementException('could not find the requested element')
# ...omitted setting up the driver and getting the page
web_element = webdriver.find_element_by_name('country_select')
select_by_text(web_element, 'Canada')
This code should click on the select element given its text or raise a NoSuchElementException exception if the given element is not a select form element or the text does not exist.
精彩评论