字符串插值TypeError:格式字符串的参数不足

时间:2016-02-24 14:28:10

标签: python python-2.7 selenium selenium-webdriver

我正在尝试将%s的Python字符串插值用于我的参数变量。我的变量是name,我希望将此变量中的值插入到放置%s的位置。我的代码段是:

def select_dataset(self, name):
    # Params name: The name of the dataset e.g. CRM
    name_element = self.get_element(By.XPATH, '//table[@id="data_configuration_datasets_ct_fields_body"]/tbody/tr/td/div/span[@title="%s" and contains(text(), "%s")]/preceding::input[1]' % name % name).click()

错误是:

 Traceback (most recent call last):
  File "C:\Webdriver\ClearCore Regression Test\ClearCore - Regression Test\TestCases\DatasetsPage_TestCase.py", line 146, in test_a2_edit_datasets
    ds.select_dataset("Edit_test")
  File "C:\Webdriver\ClearCore Regression Test\ClearCore - Regression Test\Pages\datasets.py", line 281, in select_dataset
    name_element = self.get_element(By.XPATH, '//table[@id="data_configuration_datasets_ct_fields_body"]/tbody/tr/td/div/span[@title="%s" and contains(text(), "%s") % name]/preceding::input[1]' % name).click()
TypeError: not enough arguments for format string

%s%name所在的行:

name_element = self.get_element(By.XPATH, '//table[@id="data_configuration_datasets_ct_fields_body"]/tbody/tr/td/div/span[@title="%s" and contains(text(), "%s")]/preceding::input[1]' % name % name).click()

调用方法的测试用例:

def test_a2_edit_datasets(self):
    print "*** Test edit_dataset ***"
    menu_bar = MenuBarPage(self.driver)
    tool_bar = ToolbarPage(self.driver)
    project_navigator = ProjectNavigatorPage(self.driver)
    ds = project_navigator.select_projectNavigator_item("Datasets")
    ds.click_add_datasets()
    time.sleep(1)
    ds.add_datasets_name_description_and_datamap("Edit_test", Globals.dataset_description) # adds the name, description and selects datamap drop down for the Dataset
    ds.add_name_type_and_size_for_key_fields(Globals.dataset_crm_name_keyField, Globals.dataset_type_string)
    ds.click_save_datasets()
    #ds.select_dataset(Globals.dataset_name_crm)
    ds.select_dataset("Edit_test")

我应该在哪里放%

0 个答案:

没有答案