如何参数化xpath并将其解析为文本?

How to parameterize xpath and parse it as a text?

我有一个 python 文件,Base.Py

def provideElectricJobDetails(self):
        self.ui.driver.find_element_by_name("Title").send_keys("Test")
        self.ui.driver.find_element_by_xpath("//span[contains(text(),'Select AOR...')]").click()
        self.ui.sleep(2)
        self.ui.driver.find_element_by_xpath("//li[contains(text(),'Cheyenne')]").click()
        self.ui.sleep(2)
        self.ui.driver.find_element_by_xpath("//span[contains(text(),'Submit')]").click()

还有另一个 python 文件,CreateNew.Py

def testCreateNewOtherJob(self):
        self.ui.common.clickNavbarNewJob()
        self.ui.jobs.selectServiceType('Electric')
        self.ui.jobs.selectJobType('Other')
        self.ui.jobs.ProvideElectricJobDetails()

我的问题是,不要在 Base.Py 中硬编码为 ("//span[contains(text(),'Submit')]")。 我怎样才能将它解析为文本格式,以便我可以调用 CreateNew.Py as

self.ui.jobs.selectButton("Submit")

如果我没理解错的话,你想要这样的东西:

def selectButton(name):
    self.ui.driver.find_element_by_xpath("//span[contains(text(),'" + name + "')]").click()


def testCreateNewOtherJob(self):
    self.ui.jobs.selectButton("Submit") # will click on 'Submit' button
    self.ui.jobs.selectButton("Select AOR...") # will click on 'Select AOR...' button