Skip to content Skip to sidebar Skip to footer

Variable In Xpath In Python Selenium When Calling Class And Function

I have the following class: class Sections(BasePage): CHOOSE_SECTION_SEL = (By.XPATH, '//*[@class='select2-result-label' and (text() = '' + state + '')]') def choose_sect

Solution 1:

You can change the notation to either of the following forms:

  • Using variable:

    CHOOSE_SECTION_SEL = (By.XPATH, "//*[@class='select2-result-label' and text()='" + state + "']")
    
  • Using %s

    CHOOSE_SECTION_SEL = (By.XPATH, "//*[@class='select2-result-label' and text()='%s']"% str(state))
    
  • Using {}

    CHOOSE_SECTION_SEL = (By.XPATH, "//*[@class='select2-result-label' and text()='{}']".format(str(state)))
    

You can implement it like:

classSections(BasePage):defchoose_section(self, state):
        self.CHOOSE_SECTION_SEL = (By.XPATH, "//*[@class='select2-result-label' and (text() = '" + state + "')]")
        self.click_on_element("choose section", self.CHOOSE_SECTION_SEL)

Post a Comment for "Variable In Xpath In Python Selenium When Calling Class And Function"