Search code examples
pythonseleniumpython-2.7selenium-webdriverxlrd

How to Execute the user defined python methods using variables, read from Excel sheet


I have built my reusable framework libraries for ClickLink, TypeInTextBox etc and it sits in different python modules.

When I used the methods directly in the main class like ClickLink('CP_SignIn_Link') it works fine.

But when I tried to read it from excel and use the same like below I get the following error:

Keyword(wd_handle,Arguments)

print Keyword = ClickLink
print Arguments = 'CP_SignIn_Link'

Keyword(wd_handle,Arguments,Value)

TypeError: 'unicode' object is not callable

Sample TC Excel Document:
======================

User_Keyword    | Keyword_Arguments         | Value
----------------------------------------------------------------------------------
ClickLink       | 'CP_SignIn_Link'          |
TypeInTextBox   |'CP_EmailAddress_TextBox'  |'ABC@D.COM'    
TypeInTextBox   |'CP_Password_TextBox'      |'test'

Any help would be much appreciated.


Solution

  • I tried using the globals() keyword and it worked :)

    Where User_Keyword variable will evaluate to a string equivalent to the method name in your package.

    Example: def ExecuteKeyword(driver, User_keyword, Argument) # User_keyword = ClickLink , Argument = SignInLink function = globals()[User_Keyword] function(driver,Arguments) # Equivalent to ClickLink(driver,SingnInLink) Hope this helps someone else :)