简体   繁体   English

如何在 Apache Web 服务器上安装 Selenium (python)?

[英]How to install Selenium (python) on a Apache Web Server?

I have up and running an Apache Server with Python 3.x installed already on it.我已经启动并运行了一个 Apache 服务器,上面已经安装了 Python 3.x。 Right now I am trying to run ON the server a little python program (let's say filename.py).现在我正试图在服务器上运行一个小 python 程序(比如说 filename.py)。 But this python program uses the webdriver for Chrome from Selenium.但是这个 python 程序使用来自 Selenium 的 Chrome 的 webdriver。 Also it uses sleep from time (but I think this comes by default, so I figure it won't be a problem)它也使用睡眠时间(但我认为这是默认情况下出现的,所以我认为这不会是一个问题)

from selenium import webdriver

When I code this program for the first time on my computer, not only I had to write the line of code above but also to manually download the webdriver for Chrome and paste it on /usr/local/bin.当我第一次在我的电脑上编写这个程序时,我不仅要编写上面的代码行,还要手动下载 Chrome 的 webdriver 并将其粘贴到 /usr/local/bin。 Here is the link to the file in case you wonder: Webdriver for Chorme如果您想知道,这里是文件的链接: Chorme 的 Webdriver

Anyway, I do not know what the equivalences are to configure this on my server.无论如何,我不知道在我的服务器上配置它的等价物是什么。 Do you have any idea how to do it?你知道怎么做吗? Or any concepts I could learn related to installing packages on an Apache Server?或者我可以学到与在 Apache 服务器上安装软件包相关的任何概念?

Simple solution:简单的解决方案:

You don't need to install the driver in usr/local/bin .您不需要在usr/local/bin中安装驱动程序。 You can have the.exe anywhere and you can specify that with an executable path, see here for an example.您可以在任何地方拥有 .exe,并且可以使用可执行路径指定它,请参见此处的示例。

Solution for running on a server在服务器上运行的解决方案

If you have python installed on the server, ideally >3.4 which comes with pip as default.如果您在服务器上安装了 python,理想情况下 >3.4,默认情况下随附 pip。 Then install ChromeDriver on a standalone server, follow the instructions here然后在独立服务器上安装 ChromeDriver,按照此处的说明进行操作

Note that, Selenium always need an instance of a browser to control.注意,Selenium 总是需要一个浏览器的实例来控制。

Luckily, there are browsers out there that aren't that heavy as the usual browsers you know.幸运的是,有些浏览器并不像您所知道的普通浏览器那么重。 You don't have to open IE / Firefox / Chrome / Opera.您不必打开 IE / Firefox / Chrome / Opera。 You can use HtmlUnitDriver which controls HTMLUnit - a headless Java browser that does not have any UI.您可以使用控制 HTMLUnit 的 HtmlUnitDriver - 一个没有任何 UI 的无头 Java 浏览器。 Or a PhantomJsDriver which drives PhantomJS - another headless browser running on WebKit.或者驱动 PhantomJS 的 PhantomJsDriver - 另一个在 WebKit 上运行的无头浏览器。

Those headless browsers are much less memory-heavy, usually are faster (since they don't have to render anything), they don't require a graphical interface to be available for the computer they run at and are therefore easily usable server-side.那些无头浏览器的内存要少得多,通常更快(因为它们不需要渲染任何东西),它们不需要图形界面可供它们运行的计算机使用,因此很容易在服务器端使用.

Sample code of headless setup无头设置示例代码

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)

It's also worth reading on running Selenium RC, see here on that.运行 Selenium RC 也值得一读,请参阅此处

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

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