简体   繁体   English

Python + Selenium:驱动程序路径

[英]Python + Selenium: Path to driver

Is there any possibility to run Python + Selenium script without entering the path of an exe file in every single script in Python line:是否有可能运行 Python + Selenium 脚本而不在 Python 行中的每个脚本中输入exe文件的路径:

driver = webdriver.Chrome().

The same question applies to "IE Driver", "Edge Driver" and "Gecko Driver".同样的问题适用于“IE Driver”、“Edge Driver”和“Gecko Driver”。 Can it be done by some general python class and should I create some additional file for it?可以通过一些通用的 python class 来完成吗?我应该为它创建一些额外的文件吗? Or is it a matter of Integrated Development Environment configuration?还是集成开发环境配置的问题?

I would be grateful for your expert word.我会很感激你的专家的话。

You can change the source code . 您可以更改源代码 Just assign the value of executable_path to your chromedriver path. 只需将executable_path的值分配给chromedriver路径即可。 Let me explain - 让我解释 -

When you "normally" type this - 当你“正常”输入这个 -

driver = webdriver.Chrome(r"path\chromedriver.exe")

The WebDriver object initializes in its class . WebDriver对象在其类中初始化。 The class file is located at //selenium_folder/webdriver/chrome/webdriver.py. 类文件位于//selenium_folder/webdriver/chrome/webdriver.py。 Inside it, if you notice the __init__ method, it takes an argument of executable_path . 在其中,如果您注意到__init__方法,则它接受executable_path的参数。 So you can simply do - 所以你可以简单地做 -

def __init__(self, executable_path="chromedriver", port=0,
                 options=None, service_args=None,
                 desired_capabilities=None, service_log_path=None,
                 chrome_options=None):

     executable_path = "path\chromedriver.exe"

This way, the following code will successfully run the driver - 这样,以下代码将成功运行驱动程序 -

driver = webdriver.Chrome()

No matter which OS you use you have multiple options to achieve this. 无论您使用哪种操作系统,您都有多种选择来实现这一目标。

  • first of all you can put driver file (like chorme_driver.exe ) in a relative folder to your python files. 首先,您可以将驱动程序文件(如chorme_driver.exe )放在python文件的相对文件夹中。 (this is what I normally do) (这是我通常做的)

driver = webdriver.Chrome('../chromedriver.exe')

driver = webdriver.PhantomJS('../phantomjs.exe')

  • You can put address to chrome driver inside your PATH variable in Windows, Linux or ... 您可以在Windows,Linux或...中的PATH变量中将地址放到chrome driver

driver = webdriver.Chrome('chromedriver.exe')

driver = webdriver.PhantomJS('phantomjs.exe')

  • Also you can set an environment variable and always rely on that. 您还可以设置环境变量并始终依赖于此。

driver = webdriver.Chrome(os.environ.get('CHROME_DRIVER_PATH'))

driver = webdriver.PhantomJS(os.environ.get('PHANTOMJS_DRIVER_PATH'))

Yes, you have to store the driver in the PATH. 是的,您必须将驱动程序存储在PATH中。 for example mine is at C:\\python\\python(version)\\lib\\site-package\\selenium\\webdriver and then store the driver in the proper folder. 例如我的位于C:\\python\\python(version)\\lib\\site-package\\selenium\\webdriver ,然后将驱动程序存储在适当的文件夹中。 Also make sure to add the path to your machines environmental variables. 还要确保添加机器环境变量的路径。

Here's what worked for me.这对我有用。 I put the driver file in the same folder as the app I'm coding and the line in the code looks like:我将驱动程序文件与我正在编码的应用程序放在同一个文件夹中,代码中的行如下所示:

web = webdriver.Chrome('./chromedriver.exe') web = webdriver.Chrome('./chromedriver.exe')

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

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