can't get value from yaml file in python

Viewed 164

I could not understand where is my mistake. Everything seems ok but I can't get data from yml file.
Here is my yml file:

envdev:
  base_url: url
  user_name: user
  password: password
envprod:
  base_url: url
  user_name: user
  password: password

Here is my code in python:

 with open("../env.yml", 'r') as yamlfile:
        try:
            yaml_file = yaml.safe_load(yamlfile)
        except yaml.YAMLError as exc:
            print(exc)

I am calling like this:

driver.get(yaml_file["envdev"]["base_url"])

And here is the error:

  >       raise exception_class(message, screen, stacktrace)
E       selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
E         (Session info: chrome=90.0.4430.85)

/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py:242: InvalidArgumentException
1 Answers

You can use strictyaml to do so.

yaml_file will be a YAML object and yaml_dict a dictionary.

from strictyaml import load

with open("./tests/env.yml", "r") as yamlfile:
    yaml_file = load(yamlfile.read())

yaml_file["envdev"]["base_url"]

Transform to dictionary for nicer handling:

yaml_dict = yaml_file.data
yaml_dict["envdev"]["base_url"]
Related