Hide phantomjs console

2019-08-04 12:56发布

问题:

I am doing a project and I want him to be invisible. Therefore, i used this website - http://pytoexe.com/ to create an window based exe file from my python script which means it will not use the windows console.

Unfortuently, since i am using phantomjs driver in my code, he opens a phantomjs console which interrupt me.

In order to slove this problem I need to add a line or script that prevent from the phantomjs console to appear ( changing something in my selenium files/ something like that would not work cause it probably problem in their files and i cannot doing anything with that) .

Someone know what to do?

this is my exe file

and This is my code:

from selenium import webdriver
import time
from PIL import Image
from constants import *
import operator
import os
#Email Constants
DEFAULT_CONTENT = 'example email stuff here'
HOST = 'smtp.gmail.com'
PORT = 587
EMAIL = 'freeadsandspam@gmail.com'
CUSTOMERS = []
SUBJECTS = ['new ad were found', 'ad were found by SMADS', 'best ad for you']
COMMASPACE = ', '
#Getting History
OUTPUT_FILE_PATH = 'C:\search_logger.txt'
COPY_OF_THE_HISTORY_PATH = 'C:\history'
NEW_OUTPUT_FILE_PATH = 'C:\last_search_logger.txt'
#PhantomJs And Ads Finding
PHANTOM_JS_PATH = 'C:\phantomjs-2.1.1-windows\\bin\phantomjs.exe'
OUTPUT_AD_PATH = 'ad.png'
DEFAULT_WINDOW_SIZE = (1024, 768)
AD_DATABASE = 'https://www.findads.com.au/'
KEYWORD_BUTTON_XPATH = '//*[@id="txtSearch"]'
SEARCH_BUTTON_XPATH = '/html/body/div[1]/div/form/button'
CONSTANT_TIME_SLEEPING = 3
AD_XPATH = '/html/body/div[1]/section/div/div[1]/div[4]/div[1]/div[1]/section[24]'
COMPARE_ELEMENT_XPATH = '//*[@id="fSearch"]'
CATAGORY_SORT_XPATH = '/html/body/div[1]/section/div/div[1]/div[5]/div/div[3]/form/div[1]/div[1]'



class PhantomJsDriver:
    """
    A class that taking care on the ads finding
    in the internet, doing it with PhantomJs -
    background driver
    """
    def __init__(self, ad_keyword, window_size=DEFAULT_WINDOW_SIZE, panthom_js_path=PHANTOM_JS_PATH, output_ad_path=OUTPUT_AD_PATH):
        """
        this function init our object
        in order to use the other functions
        that the object offer.

        Parameters
        ----------
        phantom_js_path : str
            path of the PhantomJs ( this essential because
            we cannot get the PhantomJs file otherwise)
        output_ad_path : str
            where you want to save the ad that the system
            had found and how you call the name of the ad
            file ( eg: ad.png )
        ad_keyword : str
            the keyword that define what ad the system bring
            ( eg: dog will bring dog ad )
        window_size : double int (int1,int2)
            define the window size of the browser ( mainly for the
            screenshot )
        """
        self.phantom_js_path = panthom_js_path
        self.output_ad_path = output_ad_path
        self.ad_keyword = ad_keyword
        self.window_size = window_size
        self.list_of_images = []
        self.dict = {}

    def get_ad(self):
        """
        this function save the ad by searching in the internet
        ( on specific website ) the keyword that the user chose
        and copy it into the output_ad_path.
        """
        for i in range(0, 5):
            driver = webdriver.PhantomJS(self.phantom_js_path)
            driver.set_window_size(self.window_size[0], self.window_size[1])
            driver.get(AD_DATABASE)
            keyword = driver.find_element_by_xpath(KEYWORD_BUTTON_XPATH)
            keyword.send_keys(self.ad_keyword)
            search_button = driver.find_element_by_xpath(SEARCH_BUTTON_XPATH)
            search_button.click()
            driver.save_screenshot("ad" + str(i) + ".png")
            element = driver.find_element_by_xpath(AD_XPATH)  # find part of the page you want image of
            self.crop_image(i, element)

    def crop_image(self, i, element):
        """
        this function crop the screenshot of the ads website from
        the previous function into one single ad.
        """
        im = Image.open("ad" + str(i) + ".png")  # uses PIL library to open image in memory
        location = element.location
        size = element.size
        left = location['x']
        top = location['y']
        right = location['x'] + size['width'] + 50
        bottom = location['y'] + size['height']
        weight, height = im.size
        print height
        im = im.crop((left, top, right, bottom))  # defines crop points
        im.save('test' + str(i) + '.png')  # saves new cropped image
        self.list_of_images.append('test' + str(i) + '.png')
        self.dict['test' + str(i) + '.png'] = 0

    def choose_the_best_ad(self):
        for img1 in self.list_of_images:
            for img2 in self.list_of_images:
                im1 = Image.open(img1)
                im2 = Image.open(img2)

                if list(im1.getdata()) == list(im2.getdata()):

                    self.dict[img1] += 1
                    self.dict[img2] += 1
        print self.dict
        BestImage = max(self.dict.iteritems(), key=operator.itemgetter(1))[0]
        print BestImage
        if os.path.exists("TheImage.png"):
            os.remove("TheImage.png")
        os.rename(BestImage, "TheImage.png")


driver = PhantomJsDriver("dog")
driver.get_ad()
driver.choose_the_best_ad()