本文整理汇总了Python中selenium.webdriver.support.ui.Select.select_by_index方法的典型用法代码示例。如果您正苦于以下问题:Python Select.select_by_index方法的具体用法?Python Select.select_by_index怎么用?Python Select.select_by_index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.webdriver.support.ui.Select
的用法示例。
在下文中一共展示了Select.select_by_index方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_can_create_meetup
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def test_can_create_meetup(self):
self.make_admin()
self.browser.get(self.live_server_url)
self.browser.add_cookie(self.create_session_cookie())
self.browser.refresh()
self.browser.get('{}{}'.format(self.live_server_url, '/meetup/add/'))
name = self.browser.find_element_by_id("id_name")
slug = self.browser.find_element_by_id("id_slug")
location = Select(self.browser.find_element_by_id("id_location"))
email = self.browser.find_element_by_id("id_email")
name.send_keys("Foo Community")
slug.send_keys("foo-community")
email.send_keys("[email protected]")
location.select_by_index(1)
# Locate the CKE iframe for description
description = self.browser.find_element_by_xpath(
"//div[contains(@id, 'cke_1_contents')]/iframe")
self.browser.switch_to.frame(description) # switch context to iframe
description_editor = self.browser.find_element_by_xpath("//body")
description_editor.send_keys("Foo description")
self.browser.switch_to_default_content() # return to main context
# Locate the CKE iframe for sponsors
sponsors = self.browser.find_element_by_xpath(
"//div[contains(@id, 'cke_2_contents')]/iframe")
self.browser.switch_to.frame(sponsors)
sponsors_editor = self.browser.find_element_by_xpath("//body")
sponsors_editor.send_keys("Foo sponsor")
self.browser.switch_to_default_content()
self.browser.find_element_by_id('submit-id-save').click()
message = self.browser.find_element_by_class_name('container').text
self.assertTrue('Meetup added Successfully' in message)
示例2: test_add_meas_order_two_item
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def test_add_meas_order_two_item(admin_client, live_server, webdriver):
selenium = webdriver()
selenium.implicitly_wait(3)
try:
create_correct_sample_data()
num_orders_before = len(MeasurementOrder.objects.all())
num_items_before = len(MeasurementItem.objects.all())
selenium.get(live_server + '/new_item_and_order/')
login_as_admin(selenium)
order_type = Select(selenium.find_element_by_id('id_order_type'))
order_type.select_by_index(1)
selenium.find_element_by_class_name('add_meas_item_btn').click()
serial_nrs = selenium.find_elements_by_id('id_serial_nr')
names = selenium.find_elements_by_id('id_name')
products = selenium.find_elements_by_id('id_product')
index = 0
for serial_nr, name, product, in zip(serial_nrs, names, products):
name.send_keys('Teddy the bear')
serial_nr.send_keys(str(4712 + index))
Select(product).select_by_index(index % 3 + 1)
index += 1
selenium.find_element_by_name('action').click()
wait_for_root_page(selenium)
assert selenium.current_url == live_server.url + '/'
assert len(MeasurementOrder.objects.all()) == num_orders_before + 1
assert len(MeasurementItem.objects.all()) == num_items_before + 2
assert MeasurementItem.objects.get(serial_nr=4712)
assert MeasurementItem.objects.get(serial_nr=4713)
finally:
selenium.quit()
示例3: backpack
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def backpack(self, item_name, pokemon=None, qty=None):
img_xpath = "//img[@src='images/pokesklep/{0}.jpg' and contains(@class, 'box-center')]".format(item_name)
# check if element is present, if not open backpack
if not is_present(By.XPATH, img_xpath):
self.menu_get(0, 2) # opens backpack from menu
# getting tab name
item_img = driver.find_element_by_xpath(img_xpath)
div = item_img.find_element_by_xpath(img_xpath+"/ancestor::div[@role='tabpanel']")
tab_id = div.get_attribute('id')
# opening backpack tab
tab_xpath = "//a[@aria-controls='{0}']".format(tab_id)
backpack_tab = driver.find_element_by_xpath(tab_xpath)
backpack_tab.click()
# waiting for tab to load and visibility of item
item_button = wait_visible(item_img)
item_button.click()
# locating form to use item
form_xpath = "//input[@value='{0}' and @name='rodzaj_przedmiotu']/parent::*".format(item_name)
form = wait_visible(driver.find_element_by_xpath(form_xpath))
# locating button to send form.
# non default quantity of item to use
if qty:
qty_input = wait_visible(form.find_element_by_xpath("input[@name='ilosc']"))
qty_input.send_keys(str(qty))
# if pokemon is specified chosing him from select menu
if pokemon:
select = Select(form.find_element_by_tag_name('select'))
select.select_by_index(pokemon-1)
button = form.find_element_by_tag_name('button')
button.click()
load_page()
示例4: test_add_meas_order_one_item
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def test_add_meas_order_one_item(admin_client, live_server, webdriver):
selenium = webdriver()
selenium.implicitly_wait(3)
try:
create_correct_sample_data()
num_orders_before = len(MeasurementOrder.objects.all())
num_items_before = len(MeasurementItem.objects.all())
selenium.get(live_server + '/new_item_and_order/')
login_as_admin(selenium)
order_type = Select(selenium.find_element_by_id('id_order_type'))
order_type.select_by_index(1)
selenium.find_element_by_name('action').click()
assert selenium.current_url == live_server.url + '/new_item_and_order/'
name = selenium.find_element_by_id('id_name')
name.send_keys('Teddy the bear')
selenium.find_element_by_name('action').click()
assert selenium.current_url == live_server.url + '/new_item_and_order/'
serial_nr = selenium.find_element_by_id('id_serial_nr')
serial_nr.send_keys('4711')
selenium.find_element_by_name('action').click()
assert selenium.current_url == live_server.url + '/new_item_and_order/'
product = Select(selenium.find_element_by_id('id_product'))
product.select_by_index(1)
selenium.find_element_by_name('action').click()
wait_for_root_page(selenium)
assert selenium.current_url == live_server.url + '/'
assert len(MeasurementOrder.objects.all()) == num_orders_before + 1
assert len(MeasurementItem.objects.all()) == num_items_before + 1
assert MeasurementItem.objects.get(serial_nr=4711)
finally:
selenium.quit()
示例5: walkProd
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def walkProd():
global driver, prevTrail
try:
# click overlay advertisement popup left button "No Thanks"
try:
driver.find_element_by_css_selector("a.btn.close.fl-left").\
click()
except (NoSuchElementException):
pass
zpath = ('#ctl00_ctl00_ctl00_mainContent_localizedContent_bodyCenter'+
'_adsPanel_lbProduct')
waitTextChanged(zpath)
curSel = Select(css(zpath))
numProds = len(curSel.options)
ulog("numProds=%d"%numProds)
startIdx = getStartIdx()
for idx in range(startIdx, numProds):
curSel = Select(css(zpath))
ulog("idx=%s"%idx)
ulog('select "%s"'%curSel.options[idx].text)
curSel.select_by_index(idx)
prevTrail+=[idx]
while True:
ret = walkFile()
if ret != TRY_AGAIN:
break
if ret== PROC_GIVE_UP:
ulog('"%s" is GIVE UP'% curSel.options[idx].text)
prevTrail.pop()
return PROC_OK
except Exception as ex:
traceback.print_exc(); ipdb.set_trace()
driver.save_screenshot('netgear_exc.png')
示例6: test_can_create_community
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def test_can_create_community(self):
self.make_admin()
self.browser.get(self.live_server_url)
self.browser.add_cookie(self.create_session_cookie())
self.browser.refresh()
self.browser.get('{}{}'.format(self.live_server_url, '/community/add_community/'))
name = self.browser.find_element_by_id("id_name")
slug = self.browser.find_element_by_id("id_slug")
order = self.browser.find_element_by_id("id_order")
email = self.browser.find_element_by_id("id_email")
mailing_list = self.browser.find_element_by_id("id_mailing_list")
parent_community = Select(
self.browser.find_element_by_id("id_parent_community"))
website = self.browser.find_element_by_id("id_website")
facebook = self.browser.find_element_by_id("id_facebook")
googleplus = self.browser.find_element_by_id("id_googleplus")
twitter = self.browser.find_element_by_id("id_twitter")
name.send_keys("Foo Community")
slug.send_keys("foo-community")
order.send_keys("5")
email.send_keys("[email protected]")
mailing_list.send_keys('[email protected]')
parent_community.select_by_index(0)
website.send_keys('http://www.foo-community.org')
facebook.send_keys('http://www.facebook.com/foo-community')
googleplus.send_keys('http://plus.google.com/foo-community')
twitter.send_keys('http://www.twitter.com/foo-community')
self.browser.find_element_by_id('submit-id-save').click()
# Wait required on this page. Tests will fail without
# wait even after successful completion of required actions.
wait = WebDriverWait(self.browser, 10)
wait.until(
EC.presence_of_element_located(
(By.XPATH, "//h1[contains(text(),'Foo Community')]")))
self.assertTrue('Foo Community' in self.browser.title)
示例7: test_drop_down
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def test_drop_down(self):
driver = self.driver
exp_options = [ "BMW", "Mercedes", "Audi", "Honda" ]
act_options = []
#Get the Dropdown as a Select using it's name attribute
make = Select(driver.find_element_by_name("make"))
#Verify Dropdown has four options for selection
self.assertEqual(4,len(make.options))
#Retrieve the option values from Dropdown using getOptions() method
for option in make.options:
act_options.append(option.text)
#Verify Dropdown has four options for selection
self.assertEqual(exp_options,act_options)
#With Select class we can select an option in Dropdown using Visible Text
make.select_by_visible_text("Honda")
self.assertEqual("Honda",make.first_selected_option.text)
#or we can select an option in Dropdown using value attribute
make.select_by_value("audi")
self.assertEqual("Audi",make.first_selected_option.text)
#or we can select an option in Dropdown using index
make.select_by_index("0")
self.assertEqual("BMW",make.first_selected_option.text)
示例8: walkProdCat
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def walkProdCat():
global driver, prevTrail
try:
# click "Drilldown"
waitClickable('#ctl00_ctl00_ctl00_mainContent_localizedContent_bodyCenter_BasicSearchPanel_btnAdvancedSearch')\
.click()
zpath = ('#ctl00_ctl00_ctl00_mainContent_localizedContent_bodyCenter_'+
'adsPanel_lbProductCategory')
curSel = Select(css(zpath))
numProdCats = len(curSel.options)
ulog('numProdCats=%d'%numProdCats)
startIdx = getStartIdx()
for idx in range(startIdx, numProdCats):
curSel = Select(css(zpath))
ulog("idx=%s"%idx)
ulog('select "%s"'%curSel.options[idx].text)
curSel.select_by_index(idx)
prevTrail+=[idx]
walkProdFam()
prevTrail.pop()
except Exception as ex:
traceback.print_exc(); ipdb.set_trace()
driver.save_screenshot('netgear_exc.png')
示例9: main
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def main(webdriver):
login(webdriver)
submit_button = webdriver.find_element_by_class_name('Submit')
submit_button.click()
try:
WebDriverWait(webdriver, int(os.environ.get('TIMEOUT'))).until(
EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'Date of birth:')]"))
)
except TimeoutException as error:
raise Exception('Timed out waiting for page to load')
multi_driver_check = webdriver.find_elements_by_xpath("//*[contains(text(), 'Date of birth:')]") # Array where len(check) will tell you how many vehicles in policy
if len(multi_driver_check) >= 2:
remove_driver_buttons = webdriver.find_elements_by_xpath('//button[text()="Remove"]')
if remove_driver_buttons and len(remove_driver_buttons) >= 2:
remove_driver_buttons[1].click()
else:
raise Exception('Could not find driver_remove button')
webdriver.find_element_by_xpath('//a[@href="/account/drivers/01/remove"]').click()
try:
WebDriverWait(webdriver, int(os.environ.get('TIMEOUT', 5))).until(
EC.presence_of_element_located((By.NAME, 'requester_name'))
)
except TimeoutException:
raise Exception('Timed out waiting for requester_name_field')
requester_name_field = Select(webdriver.find_element_by_name('requester_name'))
requester_name_field.select_by_index(1)
driver_remove_reason_field = Select(webdriver.find_element_by_name('driver_remove_reason'))
driver_remove_reason_field.select_by_visible_text('No longer lives in the household')
date_fields = webdriver.find_elements_by_name("date_input")
date_fields[0].send_keys('{}'.format(datetime.date.today() + datetime.timedelta(days=2)))
other_frequent_driver_field = Select(webdriver.find_element_by_name('other_frequent_driver'))
other_frequent_driver_field.select_by_visible_text('None')
vehicle_usage_field = Select(webdriver.find_element_by_name('vehicle_usage'))
vehicle_usage_field.select_by_visible_text('Pleasure')
vehicle_annual_kms_field = webdriver.find_element_by_name('vehicle_annual_kms')
vehicle_annual_kms_field.send_keys('90')
date_fields = webdriver.find_elements_by_name("date_input")
date_fields[0].send_keys('{}'.format(datetime.date.today() + datetime.timedelta(days=2)))
vehicle_usage_fields = webdriver.find_elements_by_xpath('//select[@name="vehicle_usage"]')
annual_kms_fields = webdriver.find_elements_by_name('vehicle_annual_kms')
if vehicle_usage_fields:
for field in vehicle_usage_fields:
select_vehicle_usage_field = Select(field)
select_vehicle_usage_field.select_by_visible_text('Pleasure')
if annual_kms_fields:
for field in annual_kms_fields:
field.send_keys('123')
for num in range(1, len(multi_driver_check) + 1):
stringified_num = '0{}'.format(num)
x_principal_driver_field = webdriver.find_elements_by_xpath("//select[@name={0}]".format("'veh_{}_principal_driver'".format(stringified_num)))
if x_principal_driver_field:
Select(x_principal_driver_field[0]).select_by_index(1)
else:
raise Exception('Can only remove driver if policy has 2+ drivers')
webdriver.close()
示例10: submitvlpform
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def submitvlpform(self, vlpform):
try:
firstname = vlpform.find_element_by_xpath(".//input[@name='first_name']")
lastname = vlpform.find_element_by_xpath(".//input[@name='last_name']")
email = vlpform.find_element_by_xpath(".//input[@name='email_address']")
phone = vlpform.find_element_by_xpath(".//input[@name='phone']")
company = vlpform.find_element_by_xpath(".//input[@name='company']")
users = Select(vlpform.find_element_by_xpath(".//select[@name='vlp_intent']"))
submit = vlpform.find_element_by_xpath(".//button")
firstname.send_keys(self.fillfirstname)
lastname.send_keys(self.filllastname)
email.send_keys(self.fillemail)
phone.send_keys(self.fillphone)
company.send_keys(self.fillcompany)
users.select_by_index(1)
if self.dosubmit:
submit.click()
self.currentlog.append('success')
self.writer.writerow(self.currentlog)
self.currentlog = []
time.sleep(2)
except (NoSuchElementException, ElementNotVisibleException) as e:
self.currentlog.append(e)
self.writer.writerow(self.currentlog)
self.currentlog = []
return 'error'
示例11: _select_text_download
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def _select_text_download(self):
"""Select text format to download"
Get dropdown element and select index/option 3 which corresponds to text
"""
mySelect = Select(self.driver.find_element_by_css_selector('#delFmt'))
mySelect.select_by_index(3)
示例12: fill_form
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def fill_form(self, **data):
if not data:
data = {
'id_name': 'name',
'id_surname': 'surname',
'id_email': '[email protected]',
'id_quantity': 1,
'id_product': 1,
}
form = self.driver.find_element_by_css_selector(
'main > form')
form_inputs = form.find_elements_by_css_selector(
'div .controls input')
form_select = Select(form.find_element_by_css_selector(
'div .controls select'))
for form_input in form_inputs:
input_id = form_input.get_attribute('id')
form_input.clear()
form_input.send_keys(data[input_id])
if self.SLOW_DOWN:
time.sleep(1)
form_select.select_by_index(data['id_product'])
if self.SLOW_DOWN:
time.sleep(1)
form.submit()
示例13: select
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def select(self, target, value):
"""
Select an option of a select box.
@param target: a element locator pointing at a select element
@param value: an option locator which points at an option of the select element
Option locators can have the following formats:
label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
example: "label=regexp:^[Oo]ther"
value=valuePattern: matches options based on their values.
example: "value=other"
id=id: matches options based on their ids.
example: "id=option1"
index=index: matches an option based on its index (offset from zero).
example: "index=2"
"""
target_elem = self._find_target(target)
tag, tvalue = self._tag_and_value(value)
select = Select(target_elem)
# the select command in the IDE does not execute javascript. So skip this command if javascript is executed
# and wait for the following click command to execute the click event which will lead you to the next page
if not target_elem.find_elements_by_css_selector('option[onclick]'):
#import pdb; pdb.set_trace()
if tag in ['label', None]:
tvalue = self._matchOptionText(target_elem, tvalue)
select.select_by_visible_text(tvalue)
elif tag == 'value':
tvalue = self._matchOptionValue(target_elem, tvalue)
select.select_by_value(tvalue)
elif tag == 'id':
option = target_elem.find_element_by_id(tvalue)
select.select_by_visible_text(option.text)
elif tag == 'index':
select.select_by_index(int(tvalue))
else:
raise UnexpectedTagNameException("Unknown option locator tag: " + tag)
示例14: scrape
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def scrape(self):
self.driver.get(PLUGINFO['url'])
select = Select(self.driver.find_element_by_name('findourpeople_searchfields$lstPracticeGroups'))
option_indexes = range(1, len(select.options))
for index in option_indexes:
select.select_by_index(index)
self.driver.find_element_by_id('findourpeople_searchfields_btnSubmitSearchQuery').click()
s = BeautifulSoup(self.driver.page_source)
r = re.compile(r'/contact-us/cvdetails-\d+.aspx\?')
x = { 'class': 'ui-button', 'href': r }
for a in s.findAll('a', attrs=x):
tr = a.findParent('tr')
td = tr.findAll('td')
# URL, name, job title and location
person = {}
person['name'] = a.text
person['url'] = urlparse.urljoin(self.driver.current_url, a['href'])
person['job_title'] = td[2].text
person['location'] = ''
print person
#
# Reselect otherwise we get a Stale Element exception
#
select = Select(self.driver.find_element_by_name('findourpeople_searchfields$lstPracticeGroups'))
self.driver.quit()
示例15: downloadFromList
# 需要导入模块: from selenium.webdriver.support.ui import Select [as 别名]
# 或者: from selenium.webdriver.support.ui.Select import select_by_index [as 别名]
def downloadFromList(driver, FROM, TO):
selector2 = driver.find_element_by_xpath("//select[@name='saveToMenu']")
options_for_second_selector = selector2.find_elements_by_tag_name("option")
for option in options_for_second_selector:
if (option.get_attribute('value') == 'other'):
option.click()
break
radio_element = driver.find_element_by_name("value(record_select_type)")
radio_element.click()
from_element = driver.find_element_by_id("markFrom")
from_element.send_keys(str(FROM))
to_element = driver.find_element_by_id("markTo")
to_element.send_keys(str(TO))
select = Select(driver.find_element_by_id('bib_fields'))
select.select_by_index(3)
select = Select(driver.find_element_by_id('saveOptions'))
select.select_by_index(1)
thing = driver.find_element_by_class_name("quickoutput-action")
thing.click()
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, 'send'))
)
exitbutton = driver.find_element_by_class_name("quickoutput-cancel-action")
exitbutton.click()