Riaz Ladhani
Riaz Ladhani

Reputation: 4062

Selenium Python check if variable value is not blank. I am getting False but there is a value

I have a function which checks if the variables are blank. If the variables do have a value I would like the function to return True. If the variables do not have a value assigned I would like the function to return False.

My function is returning False if there is a value. I want the function to return True if the variable has a value.
My logic seems incorrect. I need some help please to get it right.

My function is:

def is_view_report_showing_results(self):
    usn_row_cell1_element = self.get_element(By.XPATH, '//table[@id="reporting_view_report_dg_main_body"]/tbody/tr[1]/td[3]')
    source_fields_row_cell1_element = self.get_element(By.XPATH, '//table[@id="reporting_view_report_dg_main_body"]/tbody/tr[1]/td[4]')
    #return (usn_row_cell1_element.text is not None) and (source_fields_row_cell1_element.text is not None)
    return not (usn_row_cell1_element.text == "") and (source_fields_row_cell1_element.text == "")

self.assertTrue(reports_view_results_page.is_view_report_showing_results(), "Problem with viewing report. Please see log for details")

The value in the variable usn_row_cell1_element is a string value "2" The value in the variable source_fields_row_cell1_element is a string "Address"

I am going to try this way:

def is_view_report_showing_results3(self):
    usn_row_cell1_element = self.get_element(By.XPATH, '//table[@id="reporting_view_report_dg_main_body"]/tbody/tr[1]/td[3]')
    source_fields_row_cell1_element = self.get_element(By.XPATH, '//table[@id="reporting_view_report_dg_main_body"]/tbody/tr[1]/td[4]')
    #return (usn_row_cell1_element.text is not None) and (source_fields_row_cell1_element.text is not None)
    if (usn_row_cell1_element.text == "") and (source_fields_row_cell1_element.text == "") == False:
        return False
    else:
        return True

Thanks, Riaz

Upvotes: 0

Views: 1390

Answers (1)

alecxe
alecxe

Reputation: 473873

Simply check for the "truthiness" of the values:

return usn_row_cell1_element.text and source_fields_row_cell1_element.text

Or, if you want to pass the test if at least one of the values is non-empty, use or:

return usn_row_cell1_element.text or source_fields_row_cell1_element.text

Note that in this case the function would return a string value, but this is not a problem since, according to the docs, assertTrue would do the bool(x) is True check.

Upvotes: 2

Related Questions