简体   繁体   中英

Selenium python code to Executable program

I am using selenium chrome webdriver with python. I have this python code and I want to convert it into an executable program. I also have to use this external file of chrome web-driver. How can I do this?

from selenium import webdriver

def function():

    global driver
    driver = webdriver.Chrome(executable_path=r"C:\Users\Administrator\Desktop\AWS\chromedriver_win32\chromedriver" )
    driver.get('https://www.google.com')
    driver.close()

function()
  1. Install pip in your machine - easy_install pip
  2. Then install chromedriver by simply typing : pip install chromedriver in terminal
  3. Then navigate to automation folder, type : python nameOfTheAutomation.py This will execute your automation code.

您需要在path变量中设置将可执行文件(例如chrome驱动程序)保留在保留位置。

Two ways to achieve this.

scenario 1

You can use the location of your python file. Rather hard coding path in script you can use a relative path which will remain same every time you run your script and you can always put chromedriver.exe in same path where your python file is. using below example.

    import os

    driverpath = os.path.join(os.path.dirname(os.path.abspath(__file__)),"chromedriver.exe")
    driver = webdriver.Chrome(executable_path=driverpath)

scenario 2

You can add the chromedriver path to environment "path" variable and use below example.

    driver = webdriver.Chrome()

This will find the chromedriver from system path variables.

You can add the chromedriver path to environment variables of based on operating system, below is the link for how to add path to windows 10 environment variables.

https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM