I have a function where order and searchValue are the required parameters and action and buttonName are optional. I am using this function after running the search. This function allows the user to do different things based on the parameters..for example, clicking on the checkbox(can be anywhere in the row), click open button(anywhere in the row) etc.
here is the tr looks like(after returning three results):
here are the td inside tr(td can be checkbox, button etc)
This is the piece of code I wrote which is working fine, but want to know if I can write it in a better way or maybe I don't have to define xpath path so many times.
Here user defines the order(First or Last (tr)) and the searchValue, it will search the td with that text and perform the action in the same tr(depending on the order).
def listView(self, order, searchValue, action=False, buttonName=False):
if order = 'First':
#self.find_element("//*[starts-with(@id,'table_')]/tbody/tr")
self.find_element('//*[starts-with(@id,"table_")]/tbody/tr[1]/td[text()= "'+ searchValue +'"]')
if action == "Link":
self.click(10, "//*[starts-with(@id,'table_')]/tbody/tr[1]")
elif action == "Checkbox":
self.click(10, '//*[starts-with(@id,"table_")]/tbody/tr[1]/td/input[@type="checkbox"]')
elif action == "Button" and buttonName:
self.click(10, '//*[starts-with(@id,"table_")]/tbody/tr[1]/td/input[@type="Button"]/@value["'+ buttonName +'"]')
else:
raise Exception("Invalid value for action parameter")
elif order = 'Last':
self.find_element('//*[starts-with(@id,"table_")]/tbody/tr[last()]/td[text()= "'+ searchValue +'"]')
if action == "Link":
self.click(10, "//*[starts-with(@id,'table_')]/tbody/tr[last()]")
elif action == "Checkbox":
self.click(10, '//*[starts-with(@id,"table_")]/tbody/tr[last()]/td/input[@type="checkbox"]')
elif action == "Button" and buttonName:
self.click(10, '//*[starts-with(@id,"table_")]/tbody/tr[last()]/td/input[@type="Button"]/@value["'+ buttonName +'"]')
else:
raise Exception("Invalid value for action parameter")
else:
raise Exception("Invalid value for order parameter")