Yaml 配置文件包含 webdriver.Firefox() 方法作为参数,如何转换字符串以便将其分配给驱动程序函数

Yaml config file contains webdriver.Firefox() method as a parameter, how to convert string so it can be assigned to driver function

这是我的 yaml 文件

conf_driver: # Comment all but one to choose browser
    #driver: webdriver.Chrome()
    driver: webdriver.Firefox()
    #driver: webdriver.Edge()
    #driver: webdriver.Safari()

这是我的 python 代码

# Load YAML file configuration

with open('config.yml') as f:
    config = yaml.load(f, Loader=yaml.FullLoader)

driver = config['conf_driver']['driver']
print(driver)
driver.get(config['conf_url_member']['url]'])

这是运行我的代码

的结果
    webdriver.Firefox()
Traceback (most recent call last):
  File "C:\Users\james.potter\PycharmProjects\SBCStructure\main.py", line 43, in <module>
    driver.get(config['conf_url_member']['url]'])
AttributeError: 'str' object has no attribute 'get'

Process finished with exit code 1

我明白我本质上是在写 (string)'webdriver.Firefox()'.get()

转换从我的 yaml 配置文件中检索到的字符串的最简单方法是什么,以便它可以正确分配,就像我在做的那样:

driver = WebDriver.Firefox()

而不是

driver = "WebDriver.Firefox()"

我终于想通了,

使用了return getattr(function, 'string')

这将被解释为function.string

更深入:

def Driver_Start(browserNo):
"""
Returns 'webdriver.' + the corresponding value for browserNo
:param browserNo: Choose value: 1 = chrome, 2 = firefox, 3 = edge
:return: webdriver.config['conf_driver'][browserNo])(executable_path=config['conf_driver_path'][browserNo])
"""
return getattr(webdriver, config['conf_driver'][browserNo])(executable_path=config['conf_driver_path'][browserNo])