How to pass a parameter to a function from a class that has already a running instance with getattr - Python

Viewed 27

I have a selenium web scraping object called Bot().

In my GUI I want to call methods via an input box. I have tried:

bot = bot_module.Bot()

function_string = "somefunction(parameter)"
func_name = function_string.split("(")[0]
func_param = function_string.split("(")[-1].split(")")[0]
func = getattr(opus,func_name)
func(func_param)

This raises a valueError as defined in somefunction. Going a different way:

getattr(globals()['Bot'](),func_name)(func_param)

I get the error:

selenium.common.exceptions.SessionNotCreatedException: Message: Session is already started.

Any ideas?

1 Answers

Found the answer and sorry for spamming!

In my function I had declared:

   if param1 not in valid:
     raise ValueError(f"Param1 has to be in: {valid}")```

...and since I was passing the param as a string it raised the error.
Related