Example #1
0
    def _create_firefox(cls,
                        config,
                        driver_path,
                        browser_bin_path,
                        svc_url,
                        proxy=None):
        from selenium.webdriver import Firefox
        from selenium.webdriver import FirefoxOptions
        from selenium.webdriver import FirefoxProfile

        profile = FirefoxProfile()
        # if config["arjuna_options"]["BROWSER_PROXY_ON"]:
        #     proxy = Proxy()
        #     proxy_string = "{}.{}".format(
        #         config["arjuna_options"]["BROWSER_PROXY_HOST"],
        #         config["arjuna_options"]["BROWSER_PROXY_PORT"]
        #     )
        #     proxy.http_proxy = proxy_string
        #     proxy.ssl_proxy = proxy_string
        #     profile.set_proxy(proxy)

        caps = DesiredCapabilities.FIREFOX
        caps.update(config["driverCapabilities"])

        from arjuna import log_debug
        log_debug("Is proxy set for Firefox?: {}".format(proxy is not None))
        if proxy is not None:
            proxy.add_to_capabilities(caps)
            caps['acceptInsecureCerts'] = True

        options = FirefoxOptions()

        if browser_bin_path.lower() != "not_set":
            options.binary_location = browser_bin_path

        if cls.are_browser_prefs_set(config):
            for pref, value in config["browserPreferences"].items():
                options.set_preference(pref, value)

        if config["arjuna_options"]["BROWSER_HEADLESS"]:
            options.add_argument("-headless")

        if cls.are_browser_args_set(config):
            for arg in config["browserArgs"]:
                options.add_argument(arg)

        from selenium import webdriver
        return webdriver.Remote(svc_url,
                                browser_profile=profile,
                                options=options)
Example #2
0
    def __init_driver(self):
        if self.driver_name.lower() != "firefox":
            raise NotConfigured("SELENIUM_DRIVER_NAME must be set to 'firefox'")
        options = FirefoxOptions()
        if self.browser_executable_path:
            options.binary_location = self.browser_executable_path
        for arg in self.driver_arguments:
            options.add_argument(arg)

        self.options = options

        self.driver = Firefox(executable_path=self.driver_executable_path,
                              firefox_options=options)
        self.initialized = True
Example #3
0
    def _create_firefox(cls, config, driver_path, browser_bin_path):
        from selenium.webdriver import Firefox
        from selenium.webdriver import FirefoxOptions
        from selenium.webdriver import FirefoxProfile

        profile = FirefoxProfile()
        if config["arjunaOptions"]["BROWSER_PROXY_ON"]:
            proxy = Proxy()
            proxy_string = "{}.{}".format(
                config["arjunaOptions"]["BROWSER_PROXY_HOST"],
                config["arjunaOptions"]["BROWSER_PROXY_PORT"])
            proxy.http_proxy = proxy_string
            proxy.ssl_proxy = proxy_string
            profile.set_proxy(proxy)

        caps = DesiredCapabilities.FIREFOX
        caps.update(config["driverCapabilities"])

        options = FirefoxOptions()

        if browser_bin_path.lower() != "not_set":
            options.binary_location = browser_bin_path

        if cls.are_browser_prefs_set(config):
            for pref, value in config["browserPreferences"].items():
                options.set_preference(pref, value)

        if cls.are_browser_args_set(config):
            for arg in config["browserArgs"]:
                options.add_argument(arg)

        # print(options.to_capabilities())
        # caps[FirefoxOptions.KEY] = options.to_capabilities()[FirefoxOptions.KEY]
        driver = Firefox(executable_path=driver_path,
                         firefox_profile=profile,
                         capabilities=caps)

        if cls.are_extensions_set(config):
            for ext in config["browserExtensions"]:
                driver.install_addon(ext)

        return driver
Example #4
0
    def create_browser(self) -> bool:
        try:
            self.display = Display(backend="xvnc", size=(self.x, self.y), rfbport=0)
            self.display.start()
        except Exception as e:
            logger.warning("[{}][{}] Cannot create virtual display: {}".format(
                self.request.client_id, self.task_hash, e))
            return False

        try:
            self.temp_dir = mkdtemp(dir=self.config.params.default.firefox_profiles_dir,
                                    prefix="task_{}_".format(self.task_hash))
        except Exception as e:
            logger.warning("[{}][{}] Cannot create temporary directory: {}".format(
                self.request.client_id, self.task_hash, e))
            return False

        options = FirefoxOptions()
        options.binary_location = self.config.params.default.firefox_path

        # add application related arguments.
        options.add_argument("--new-instance")
        options.set_preference("browser.link.open_newwindow", 3)  # open urls in tabs, not in windows.
        options.set_preference("dom.webnotifications.enabled", False)  # disable all notifications.
        options.set_preference("media.autoplay.default", 5)  # disable all media autoplaying.

        # add user-defined arguments.
        for argument in self.request.browser.argument:
            try:
                options.add_argument(argument)
            except InvalidArgumentException:
                logger.warning("[{}][{}] Invalid argument: {}".format(
                    self.request.client_id, self.task_hash, argument))
                continue

        # create browser.
        try:
            log = os.path.join(self.temp_dir, "geckodriver.log")

            self.browser = webdriver.Firefox(executable_path=FIREFOX_GECKODRIVER_WRAPPER,
                                             firefox_binary=self.config.params.default.firefox_path,
                                             log_path=log,
                                             options=options,
                                             seleniumwire_options=self.selenium_wire_options,
                                             service_args=[
                                                 "--log", "trace",
                                                 self.config.params.default.firefox_driver_path,  # must be before last
                                                 self.temp_dir                                    # must be last
                                             ])
        except WebDriverException as e:
            logger.error("[{}][{}] Cannot create browser: {}".format(self.request.client_id, self.task_hash, e))
            return False

        # add extensions.
        for extension in self.request.browser.extension:
            try:
                self.browser.install_addon(
                    os.path.join(self.config.params.default.firefox_extensions_dir, extension.strip()))
            except Exception as e:
                logger.warning("[{}][{}] Invalid extension: {}, {}".format(
                    self.request.client_id, self.task_hash, extension.strip(), e))
                continue

        # set geometry.
        self.browser.set_window_size(self.x, self.y)

        # set timeouts.
        self.browser.set_page_load_timeout(self.request.browser.page_timeout)
        self.browser.set_script_timeout(self.request.browser.script_timeout)

        return True