I'm trying to automate regression testing for basic html websites using selenium-webdriver using python. I wrote a function that will take input from an excel to determine the type of locator of an element on the webpage which goes like this:
if locator_type == "ID":
web_element = self.__driver.find_element_by_id(element)
elif locator_type == "NAME":
web_element = self.__driver.find_element_by_name(element)
elif locator_type == "XPATH":
web_element = self.__driver.find_element_by_xpath(element)
elif locator_type == "TAG_NAME":
web_element = self.__driver.find_element_by_tag_name(element)
elif locator_type == "LINK_TEXT":
web_element = self.__driver.find_element_by_link_text(element)
elif locator_type == "CLASS_NAME":
web_element = self.__driver.find_element_by_class_name(element)
elif locator_type == "PARTIAL_LINK_TEXT":
web_element = self.__driver.find_element_by_partial_link_text(element)
This is so that the we could specify the locator type and give actual locator('element') so that selenium could try to find the web element on the web-page. Is there any way to reduce the elif statements or any other better way to write this part?
I tried the method in the above link but it didn't help me. Kindly help me resolve this.
EDIT I tried create a dict like this
locator_dict = {
'ID' : driver.find_element_by_id(element),
'NAME' : driver.find_element_by_name(element)}
then i received an error saying that element is not defined