1

I made a script which auto logins to a website and made a .exe file with pyinstaller. Is there a way to implement the chromedriver into this .exe file, so it doesn't have to be installed when I wanna use my programm on another computer? I don't know if it's necessary, but here's my code:

from selenium import webdriver
import time

try:
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation", "--load-extension"])
    browser = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=chrome_options)
    browser.maximize_window()
    browser.get("http://website.example")
    browser.switch_to.frame(browser.find_element_by_xpath("/html/frameset/frame[1]"))
    browser.find_element_by_xpath("/html/body/center/table/tbody/tr[5]").click()
    time.sleep(0.5)
    browser.switch_to.default_content()
    browser.switch_to.frame(browser.find_element_by_xpath("/html/frameset/frame[2]"))
    browser.find_element_by_xpath("/html/body/p[2]/table/tbody/tr[4]/td[1]/form/input[1]").click()
    time.sleep(0.5)
    browser.find_element_by_xpath("/html/body/p[2]/table/tbody/tr[4]/td[1]/form/input[1]").send_keys("the password")
    browser.find_element_by_xpath("/html/body/p[2]/table/tbody/tr[4]/td[1]/form/input[2]").click()
except:
    pass

and I have the chromedriver.exe in the same directory

CaptainSword
  • 185
  • 1
  • 7

1 Answers1

0

The Chromedriver.exe itself is a binary file, so you would simply have to define the chromedriver's path on the local system and the desired location relative to the dist\myscript should be defined, so it would look something like this:

Example file - myscript.spec

a = Analysis(['myscript.py'],
             pathex=['path\\to\\my\\script'],
             binaries=[ ('path\\to\\my\\chromedriver.exe', '.\\selenium\\webdriver') ],
             datas=None,

You would then simply run pyinstaller with this spec file: pyinstaller myscript.spec myscript.py

Here is a comprehensive walkthrough from a similar question

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • and which path do I have to use in the .py file for the chromedriver? – CaptainSword May 05 '20 at 21:39
  • A relative path since the absolute path will not be valid when you run on another computer. – AzyCrw4282 May 05 '20 at 21:40
  • I tried it with just "chromedriver.exe" like in my code, but the chromedriver.exe has to be in the same directory as the script.exe to work, this is the script.spec ```...a = Analysis(['script.py'], pathex=['C:\\Users\\User\\PycharmProjects\\test'], binaries=[ ("C:\\Users\\User\\PycharmProjects\\test\\chromedriver.exe", ".\\selenium\\webdriver") ], datas=[],...``` – CaptainSword May 05 '20 at 21:52
  • So in which dir do you want the `chromedriver.exe` to be in? – AzyCrw4282 May 05 '20 at 22:06
  • That doesn't matter, it just should be running on another computer which hasn't installed chromedriver – CaptainSword May 05 '20 at 22:35
  • [see this](https://stackoverflow.com/a/47780274/6505847) for that. However, do note that chrome driver often has compatible issues so assuming your version would work on another computer is not the best way to go about it. My approach would be to do it the way you have and if there's compatibility issues then force the user to replace the chromedriver with the required version. So here you would need to pass around a folder which contains the `chromedriver` and the `.exe` file – AzyCrw4282 May 05 '20 at 22:50