python - 类型错误 : unsupported operand type(s) for %: 'tuple' and 'str'

标签 python selenium

我有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 % (INDEX_MAP[index])

作者:

MainPageLocatars.TEST % (INDEX_MAP[index])

进行字符串格式化。

关于python - 类型错误 : unsupported operand type(s) for %: 'tuple' and 'str' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41576708/

相关文章:

python - 根据其他列的值将新列添加到数据框

python - 如何在 python 2.x 交互模式下将参数传递给模块

headless 模式下的 Selenium FindElement 和 Chrome

Python Selenium 未加载整页源代码

selenium - Selenium 标识符的建议命名约定

python - 响应后记录请求已被响应

python - 应用装饰器的弃用

python - 使用成对后进先出方法作为条件创建 DataFrame 列

javascript - 如何在 angular2 中实现间隔/轮询以使用 Protractor ?

c# - API 测试的测试自动化