0

I am getting the chromedriver.exe from a path from my pc:

executable_path='C:\programs\chromedriver.exe'

Is there a way to get it from the current source direcotry for my python project which is called 'SRC' Basically can it be done using a relative path not an absolute one, so when i move the whole project (SRC folder) to another PC to be able to run the same webdriver?

michael
  • 7
  • 5
  • did you try it and it didn't work? – Charalamm Sep 01 '20 at 14:44
  • Does this answer your question? [How to properly determine current script directory?](https://stackoverflow.com/questions/3718657/how-to-properly-determine-current-script-directory) – RichieV Sep 01 '20 at 14:51

2 Answers2

1

Assuming your directory tree is something like that:

SRC
│   script.py
│   chromedriver.exe

In your script.py, you can:

import os

script_dir = os.path.dirname(os.path.realpath(__file__))
executable_path = os.path.join(script_dir, "chromedriver.exe")
Or Y
  • 2,088
  • 3
  • 16
  • Yup, did the trick, if you want, can u give me an example if the proect struct was abit more nested, like `dir1/dir2/SRC` ? – michael Sep 01 '20 at 14:49
  • @michael It would still work, as long as `chromedriver.exe` is next to your script. – Or Y Sep 01 '20 at 14:50
  • @michael If you want to give me a specific case where the files are located in separate locations, I can give a bit different example – Or Y Sep 01 '20 at 14:52
0

Use Webdriver manager here https://pypi.org/project/webdriver-manager/ no need to worry about maintaining required chromedriver.exe file in the path. This will download the required driver specified in the code.

Shrini
  • 193
  • 10