为%:' tuple'获取错误不支持的操作数类型和' str'

时间:2017-01-12 14:55:57

标签: python selenium

我有locators.py

class MainPageLocatars(object):
  # Login function locators
  LOGFILELOCATOR     = (By.XPATH, '//a[contains(@href, "%s")]/./../../td[7]')

我正在调用此定位器,如下所示:

from locators import *

def autoload(self, subFolder, fileName, logFile):
        # change made below
        beforeDate = self.find_element(MainPageLocatars.LOGFILELOCATOR % logFile).text

这是正确的方法吗?

这是我得到的错误:

   beforeDate = self.driver.find_element_by_xpath((AutoLoaderLocatars.LOGFILELOCATOR) % logFile).text
TypeError: unsupported operand type(s) for %: 'tuple' and 'str'

1 个答案:

答案 0 :(得分:1)

MainPageLocatars.LOGFILELOCATOR是一个元组:实际上它包含两个元素。元组上没有定义%运算符。

你应该解决并处理结果:

def autoload(self, subFolder, fileName, logFile):
    #first obtain the two elements in the tuple
    (a,b) = MainPageLocatars.LOGFILELOCATOR
    #next find the element and use the % operator in the string
    beforeDate = self.find_element((a,b%logFile)).text