【问题标题】:TypeError: unsupported operand type(s) for %: 'tuple' and 'str'TypeError:不支持的操作数类型 %:'tuple' 和 'str'
【发布时间】:2017-01-10 19:20:41
【问题描述】:

我有 locators.py

class MainPageLocatars(object):
  # Login function locators
  TEST           = "//*[starts-with(@id,'table_')]/tbody/tr[%s]"

我称这个定位器如下:

INDEX_MAP = {
    'First': '1',
    'Last': 'last()'
}

# all locaters for this class are defined here only
class ListView(Page):

    def __init__(self, driver, index):

        if index not in INDEX_MAP:
            raise ValueError("Invalid index %s" % index)

        self.driver = driver
        self.row = self.driver.find_element_by_xpath(MainPageLocatars.FRAMEONE % (INDEX_MAP[index])

这是正确的做法吗?

这是我得到的错误:

   self.row = self.driver.find_element_by_xpath(MainPageLocatars.FRAMEONE % (INDEX_MAP[index]))
   self.row = self.driver.find_element_by_xpath(MainPageLocatars.FRAMEONE % (INDEX_MAP[index]))
TypeError: unsupported operand type(s) for %: 'tuple' and 'str'

【问题讨论】:

  • 显然MainPageLocatars.FRAMEONE 是一个元组。你期待% 对它做什么?
  • 根据贴出的代码,MainPageLocatars 甚至没有FRAMEONE 属性...
  • 尝试在...MainPageLocatars.FRAMEONE % (INDEX_MAP[index],))末尾添加一个“逗号”。
  • @RafaelAguilar 那么他可能会得到TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'

标签: python selenium


【解决方案1】:

替换:

MainPageLocatars.FRAMEONE % (INDEX_MAP[index])

作者:

MainPageLocatars.TEST % (INDEX_MAP[index])

进行字符串格式化。

【讨论】:

  • 哦,我不知道我为什么要添加 frameone。多谢。它有效。
猜你喜欢
  • 1970-01-01
  • 2013-02-20
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 2018-04-22
相关资源
最近更新 更多