在OpenShift上使用Python,Selenium和Phantomjs(拒绝插座绑定权限?)



好吧,我正处于束缚的结尾,试图让phantomjs在OpenShift环境中与硒一起使用。我已经使用SSH下载了Phantomjs二进制文件,甚至可以在外壳中运行它。但是,当使用硒启动WebDriver服务时,无论我投入什么args,我都会遇到此回溯错误。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/lib/openshift/576e22027628e1fb13000211/python/virtenv/venv/lib/python3.3/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 50, in __init__
    service_args=service_args, log_path=service_log_path)
  File "/var/lib/openshift/576e22027628e1fb13000211/python/virtenv/venv/lib/python3.3/site-packages/selenium/webdriver/phantomjs/service.py", line 50, in __init__
    service.Service.__init__(self, executable_path, port=port, log_file=open(log_path, 'w'))
  File "/var/lib/openshift/576e22027628e1fb13000211/python/virtenv/venv/lib/python3.3/site-packages/selenium/webdriver/common/service.py", line 33, in __init__
    self.port = utils.free_port()
  File "/var/lib/openshift/576e22027628e1fb13000211/python/virtenv/venv/lib/python3.3/site-packages/selenium/webdriver/common/utils.py", line 36, in free_port
    free_socket.bind(('0.0.0.0', 0))
PermissionError: [Errno 13] Permission denied

不确定发生了什么事,我应该绑定到IP地址吗?如果是这样,我尝试使用服务ARGS,但没有帮助。

我遇到了同样的问题,试图在我的openshift托管托管django应用程序上运行phantomjs,并在python 3齿轮上运行。最后,我设法使它起作用,这就是:

要克服的主要问题是OpenShift不允许应用程序绑定Localhost(NOR'0.0.0.0'nor'127.0.0.1)。因此,重点是绑定到OpenShift Gear的实际IP地址

您必须在Ghostdriver级别以及Python-Selenium Binding中处理此问题。

ghostdriver(phantomjs二进制)

不幸的是,正如保罗·伯纳迪(Paolo Bernardi)在这篇文章中出色地解释的那样:http://www.bernardi.cloud/2015/02/02/25/phantomjs-withomjs-with-with-ghostdriver-on-openshift/您必须使用一个修补版的Phantomjs版本为此,由于发布版本不允许绑定到指定的IP。Paolo链接的二进制文件无法在我的Python3 Cardridge上使用,但这是很好的工作:https://github.com/jrestful/server/server/server/blob/blob/master/seo/phantomjs-1.9.9.8-patched.tar。gz?raw = true (请参阅试图在OpenShift上运行phantomjs的问题:无法修补GhostDriver,以便可以在服务器IP地址绑定以获取详细信息)

将此phantomjs二进制上传到app-root/data/phantomjs/bin(例如),并确保其运行:

> chmod 711 app-root/data/phantomjs/bin/phantomjs

您现在可以检查一下您可以像这样的IP键(我选择了我的应用程序端口号15002,我认为您可以选择所需的任何值15000):

> echo $OPENSHIFT_PYTHON_IP
127.13.XXX.XXX
> app-root/data/phantomjs/bin/phantomjs --webdriver=127.13.XXX.XXX:15002
PhantomJS is launching GhostDriver...
[INFO  - 2017-03-24T13:16:36.031Z] GhostDriver - Main - running on port 127.13.XXX.XXX:15002

好吧,现在杀死此过程并继续步骤2:Python Webdriver

phantomjs

的自定义python-selenium webdriver

重点是将IP地址添加到bintomjs webdriver的参数。

首先,我定义了新设置以适应我的设置中的OpenShift的约束。py

PHANTOMJS_BIN_PATH = os.path.join(os.getenv('OPENSHIFT_DATA_DIR'), 'phantomjs', 'bin', 'phantomjs')
PHANTOMJS_LOG_PATH = os.path.join(os.getenv('OPENSHIFT_LOG_DIR'), 'ghostdriver.log')

(确保App-root/logs/是可写的,也许您必须将其chmod)

然后,我必须覆盖Phantomjs WebDriver类,以作为参数提供IP地址。这是我自己的实现:

from selenium.webdriver import phantomjs
from selenium.webdriver.common import utils
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class MyPhantomJSService(phantomjs.service.Service):
    def __init__(self, executable_path, port=0, service_args=None, log_path=None, ip=None):
        if ip is None:
            self.ip = '0.0.0.0'
        else:
            self.ip = ip
        phantomjs.service.Service.__init__(self, executable_path, port, service_args, log_path)
    def command_line_args(self):
        return self.service_args + ["--webdriver=%s:%d" % (self.ip, self.port)]
    def is_connectable(self):
        return utils.is_connectable(self.port, host=self.ip)
    @property
    def service_url(self):
        """
        Gets the url of the GhostDriver Service
        """
        return "http://%s:%d/wd/hub" % (self.ip, self.port)

class MyPhantomWebDriver(RemoteWebDriver):
    """
    Wrapper to communicate with PhantomJS through Ghostdriver.
    You will need to follow all the directions here:
    https://github.com/detro/ghostdriver
    """
    def __init__(self, executable_path="phantomjs",
                 ip=None, port=0, desired_capabilities=DesiredCapabilities.PHANTOMJS,
                 service_args=None, service_log_path=None):
        """
        Creates a new instance of the PhantomJS / Ghostdriver.
        Starts the service and then creates new instance of the driver.
        :Args:
         - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
         - ip - IP sur lequel on veut se binder : c'est la spécificité de ce monkeypatch
         - port - port you would like the service to run, if left as 0, a free port will be found.
         - desired_capabilities: Dictionary object with non-browser specific
           capabilities only, such as "proxy" or "loggingPref".
         - service_args : A List of command line arguments to pass to PhantomJS
         - service_log_path: Path for phantomjs service to log to.
        """
        self.service = MyPhantomJSService(
            executable_path,
            port=port,
            service_args=service_args,
            log_path=service_log_path,
            ip=ip)
        self.service.start()
        try:
            RemoteWebDriver.__init__(
                self,
                command_executor=self.service.service_url,
                desired_capabilities=desired_capabilities)
        except Exception:
            self.quit()
            raise
        self._is_remote = False
    def quit(self):
        """
        Closes the browser and shuts down the PhantomJS executable
        that is started when starting the PhantomJS
        """
        try:
            RemoteWebDriver.quit(self)
        except Exception:
            # We don't care about the message because something probably has gone wrong
            pass
        finally:
            self.service.stop()

最后,调用此自定义webdriver而不是webdriver.phantomjs(...,如下:

from .myphantomjs import MyPhantomWebDriver
browser = MyPhantomWebDriver(executable_path=settings.PHANTOMJS_BIN_PATH, service_log_path=settings.PHANTOMJS_LOG_PATH, ip=os.getenv('OPENSHIFT_PYTHON_IP'), port=15002)

从那时起,您可以使用浏览器对象通常

最新更新