-4

I Tried adding a variable inside a dictionary, in a triple qoutes strings, but the value of the variable is not given.

My Code

os= 'Linux'
driver.execute_cdp_cmd(
    "Page.addScriptToEvaluateOnNewDocument",
    {
        "source": """
            Object.defineProperty(navigator, 'platform', {get: () => os});
            
            """
    }
)

please what's the right way to add my varriable

  • 1
    Why would you expect it to magically pick up `os` as a variable? What if you had a variable called `source` or `navigator` or `get` or even `platform`? – luk2302 Jan 03 '23 at 08:45
  • Do you have any idea on How to add variable inside a dictionary, in a Triple quotes strings Python – alimayorru Jan 03 '23 at 09:12

1 Answers1

0

Edit: due to {get () => ... } string formatting breaks. so i split the strings

os = 'Linux'
driver.execute_cdp_cmd(
    "Page.addScriptToEvaluateOnNewDocument",
    {
        "source": """
            Object.defineProperty(navigator, 'platform', {get: () => """+os+"""});
        """
    }
)
mohan
  • 54
  • 1
  • 4
  • 3
    Please spend a bit more time writing more complete answers instead of going for the [fastest gun in the west](https://meta.stackexchange.com/questions/9731/fastest-gun-in-the-west-problem) - e.g. proper syntax highlighting, actually applying the f-string to the actual question, etc. – luk2302 Jan 03 '23 at 08:46
  • oops. realized the problem. this should work. – mohan Jan 03 '23 at 09:04
  • https://stackoverflow.com/a/74990932/9773315 Didn't work, but it works if i put the Variable value ("Linux") – alimayorru Jan 03 '23 at 09:44