简体   繁体   English

Selenium Python代码到可执行程序

[英]Selenium python code to Executable program

I am using selenium chrome webdriver with python. 我正在将selenium chrome webdriver与python一起使用。 I have this python code and I want to convert it into an executable program. 我有此python代码,我想将其转换为可执行程序。 I also have to use this external file of chrome web-driver. 我还必须使用chrome网络驱动程序的外部文件。 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 在您的机器上安装pip-easy_install pip
  2. Then install chromedriver by simply typing : pip install chromedriver in terminal 然后只需输入以下内容即可安装chromedriver:在终端中pip install chromedriver
  3. Then navigate to automation folder, type : python nameOfTheAutomation.py This will execute your automation code. 然后导航到自动化文件夹,输入:python nameOfTheAutomation.py这将执行您的自动化代码。

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

Two ways to achieve this. 实现此目的的两种方法。

scenario 1 方案1

You can use the location of your python file. 您可以使用python文件的位置。 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. 您可以使用相对路径来代替脚本中的硬编码路径,该相对路径在每次运行脚本时都将保持不变,并且始终可以将chromedriver.exe放在python文件所在的相同路径中。 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 方案2

You can add the chromedriver path to environment "path" variable and use below example. 您可以将chromedriver路径添加到环境“ path”变量,并使用以下示例。

    driver = webdriver.Chrome()

This will find the chromedriver from system path variables. 这将从系统路径变量中找到chromedriver。

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. 您可以将chromedriver路径添加到基于操作系统的环境变量中,以下是如何向Windows 10环境变量添加路径的链接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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