我们从Python开源项目中,提取了以下36个代码示例,用于说明如何使用pyvirtualdisplay.Display()。
def launch(self): """launch browser and virtual display, first of all to be launched""" try: # init virtual Display self.vbro = Display() self.vbro.start() logger.debug("virtual display launched") except Exception: raise exceptions.VBroException() try: self.browser = Browser(self.brow_name) logger.debug(f"browser {self.brow_name} launched") except Exception: raise exceptions.BrowserException( self.brow_name, "failed to launch") return True
def main(username, account_password,destination): profile = webdriver.FirefoxProfile() profile.set_preference('browser.download.folderList', 2) # custom location profile.set_preference('browser.download.manager.showWhenStarting', False) profile.set_preference('browser.download.dir', destination) profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "image/png,image/jpeg") if not username == "NONE" and not account_password == "NONE" and not destination == "NONE": display = Display(visible=0, size=(800, 600)) display.start() driver = webdriver.Firefox(firefox_profile=profile) driver.get("https://www.facebook.com") email_id = driver.find_element_by_id("email") password = driver.find_element_by_id("pass") email_id.send_keys(username) password.send_keys(account_password) driver.find_element_by_id("loginbutton").click() # driver.find_element_by_css_selector("._5afe.sortableItem").click() driver.find_element_by_id("navItem_2305272732").click() time.sleep(3) list_of_images = driver.find_elements_by_css_selector(".uiMediaThumbImg") list_of_images[0].click() # print list_of_images for image in list_of_images: time.sleep(3) driver.find_element_by_xpath("//div[@class = 'overlayBarButtons rfloat _ohf']/div/div/a").click() time.sleep(3) option = driver.find_element_by_xpath("//div[@class = 'uiContextualLayerPositioner uiLayer']/div/div/div[@class = '_54ng']/ul[@class = '_54nf']/li[4]/a") option_name = option.find_element_by_xpath(".//*") option_name = option_name.find_element_by_xpath(".//*") if option_name.get_attribute('innerHTML').lower() == "download": option_name.click() # print option.get_attribute('innerHTML') driver.find_element_by_css_selector(".snowliftPager.next.hilightPager").click() display.stop() else: print "\nIncomplete Parameters, Program is Shutting Down."
def setUpClass(cls): if not cls.display_browser_window(): display_args = {'visible': False} if cls.browser_window_size is not None: # For some browsers, we need display to be bigger # than the size we want the window to be. width, height = cls.browser_window_size display_args['size'] = (width + 500, height + 500) cls.__display = Display(**display_args) cls.__display.start() # We have one driver attached to the class, re-used between test runs # for speed. Manually started driver instances (using new_browser_session) # are cleaned up at the end of an individual test. cls._cls_driver = cls._create_browser_instance() super(FuncSeleniumMixin, cls).setUpClass()
def pollute_forever(self): if self.verbose: print("""Display format: Downloading: website.com; NNNNN links [in library], H(domain)= B bits [entropy] Downloaded: website.com: +LLL/NNNNN links [added], H(domain)= B bits [entropy] """) self.open_driver() self.seed_links() self.clear_driver() if self.quit_driver_every_call: self.quit_driver() while True: # pollute forever, pausing only to meet the bandwidth requirement try: if (not self.diurnal_flag) or self.diurnal_cycle_test(): self.pollute() else: time.sleep(self.chi2_mean_std(3.,1.)) if npr.uniform() < 0.005: self.set_user_agent() # reset the user agent occasionally self.elapsed_time = time.time() - self.start_time self.exceeded_bandwidth_tasks() self.random_interval_tasks() self.every_hour_tasks() time.sleep(self.chi2_mean_std(0.5,0.2)) except Exception as e: if self.debug: print('.pollute() exception:\n{}'.format(e))
def getDriverWithProxySupport(self, proxy_host, proxy_port): if self.debug == False: self.display = Display(visible=0, size=(1920, 1080)) self.display.start() profile = self.getWebDriverProfile() profile.set_preference("network.proxy.type", 1) profile.set_preference("network.proxy.http", proxy_host) profile.set_preference("network.proxy.http_port", proxy_port) profile.set_preference("network.proxy.https", proxy_host) profile.set_preference("network.proxy.https_port", proxy_port) profile.set_preference("network.proxy.ssl", proxy_host) profile.set_preference("network.proxy.ssl_port", proxy_port) profile.update_preferences() capabilities = webdriver.DesiredCapabilities().FIREFOX capabilities["marionette"] = False newdriver = webdriver.Firefox(firefox_profile=profile, capabilities=capabilities) #newdriver = webdriver.Firefox(firefox_profile=profile) self.wait = ui.WebDriverWait(newdriver, 10) # timeout after 10 seconds return newdriver
def __init__( self, bid, sleep_time, threshold, printq ): self.bid = bid self.sleep_time = sleep_time self.threshold = threshold self.emails = [] self.passwords = [] self.printq = printq self.count = 0 self.finished = False self.display = Display( visible=0, size=( 1280, 800 ) ) self.display.start() self.browser = webdriver.Firefox() self.printq.put( "[>] Initialized browser {}".format( str( self.bid ) ) )
def get_ans_from_zd_webdriver(question): """ ?????????????????????? :param question: :return: """ global zhidaoHeader global ZHIDAO # invisualble # display = Display(visible=0, size=(800, 600)) # display.start() url_q_list = ZHIDAO + "/index?rn=10&word=" + question driver = webdriver.Firefox() driver.get(url_q_list) content = driver.find_element_by_xpath('/html/body/div[3]/p[1]/a') return None
def __init__(self): self.display = Display(visible=0, size=(800, 600)) #??????? self.display.start() # self.chromedriver = "/home/youmi/Downloads/chromedriver" # self.driver = webdriver.Chrome(self.chromedriver) #??display??????? chromedriver = "/home/ubuntu/geckodriver" self.driver = webdriver.Firefox(executable_path=chromedriver)
def __init__(self): self.display = Display(visible=0, size=(800, 600)) #??????? self.display.start() # chromedriver = "/home/youmi/Downloads/chromedriver" # self.driver = webdriver.Chrome(chromedriver) #??display??????? chromedriver = "/home/ubuntu/geckodriver" self.driver = webdriver.Firefox(executable_path=chromedriver)
def __init__(self, username, password, virtual=True): self.username = username self.password = password self.display = Display(visible=virtual, size=(1024, 768))
def __init__(self, is_desktop): """ ??? is_desktop : ?????? ??????, ??????(GUI)?? ??????\ (true, false) """ # config.ini??? ?? ???? config = configparser.ConfigParser() config.read('config.ini') self.apk_directory = config.get('Setting','APK_DIRECTORY') self.is_desktop = is_desktop # ????? ?????? ??????? ?? if(not is_desktop): self.display = Display(visible=0, size=(800, 600)) self.display.start() # ?? ???? ?? self.chrome = webdriver.Chrome(config.get('Setting',\ 'CHROME_DRIVER_DIRECTORY')) # ???? ???? ??? ?? self.category_list = config.items('PlayStoreURL') # ???? ???? ??? DBController?? ?? self.db_connector = DBController(config.get('Setting','DB_DIRECTORY')) # ?????? ??? SQLite ??? ?? self.db_connector.create_table()
def setUpClass(cls): cls.display = Display(visible=0, size=(1600, 1200)) cls.display.start() cls.driver = webdriver.Firefox() #cls.driver = webdriver.Chrome() # requires chromedriver in path and pip installed #cls.driver = webdriver.PhantomJS()
def init_driver_headless(): ''' Initialize headless browser. it needs a virtual display ''' from pyvirtualdisplay import Display global Display # global variable because its needed in quit_driver() display = Display(visible = 0, size = (1024, 768)) display.start() print "display initialized for headless browser" driver = webdriver.Firefox() return driver
def __init__(self, **kwargs): self.display = Display(visible=0, size=(800, 600)) self.display.start() self._experiment = False self._depth = -1 # signal self.onfinish = Finish() super(HeadlessBrowser, self).__init__(**kwargs)
def __init__(self, username, password, url): self.username = username self.password = password self.url = url self.display = Display(visible=0, size=(1920, 1080)) self.display.start() self.browser = webdriver.Chrome() self.browser.implicitly_wait(15)
def get_final_url(url): with Display(backend="xvfb", size=(1440, 900)): driver = webdriver.Chrome() driver.maximize_window() driver.get(url) url = driver.current_url driver.quit() return url
def set_debug(self): if self.debug == 'yes': value = 1 else: value = 0 # not display window browser self.display = Display(visible=value, size=(1440, 900)).start()
def display(self): display = getattr(self, "_display", None) if display is None: # http://coreygoldberg.blogspot.com/2011/06/python-headless-selenium-webdriver.html display = Display(visible=0, size=(800, 600)) display.start() self._display = display return display
def _driver(self): if self.virtual: self.display = Display(visible=0, size=(1024, 786)) self.display.start() service_args = [ '--webdriver-loglevel=ERROR' # only record ERROR message '--proxy=127.0.0.1:3128', ] # setting Chrome option os.environ['webdriver.chrome.driver'] = self.driver_path prefs = {'download.default_directory': self.download_path_temp} proxy = '127.0.0.1:3128' chrome_options = Options() chrome_options.add_experimental_option('prefs', prefs) chrome_options.add_argument('--proxy-server=http://%s' % proxy) driver = webdriver.Chrome( executable_path=self.driver_path, # ?? dirver ?? service_log_path=self.logs_path, # ?? log ???? chrome_options=chrome_options, # ???????? #service_args=service_args, ) return driver
def get_chrome_driver(self): display = None driver = None try: options = webdriver.ChromeOptions() proxy = get_proxy() if proxy: options.add_argument('--proxy-server=' + proxy) else: return None # proxy is essential display = Display(visible=0, size=(800, 800)) display.start() driver = webdriver.Chrome(executable_path=r"/home/lxw/Software/chromedriver_selenium/chromedriver", chrome_options=options) # ?????? driver.set_page_load_timeout(self.TIMEOUT) driver.set_script_timeout(self.TIMEOUT) # ??????????? except Exception as e: self.error_logger.error("lxw get_chrome_driver() Exception: {0}\n{1}\n\n".format(e, "--"*30)) if display: display.stop() if driver: driver.quit() return None, None else: return display, driver
def get_chrome_driver(self): # chromedriver display = None driver = None try: options = webdriver.ChromeOptions() proxy = get_proxy() if proxy: options.add_argument('--proxy-server=' + proxy) else: return None # proxy is essential display = Display(visible=0, size=(800, 800)) display.start() driver = webdriver.Chrome(executable_path=r"/home/lxw/Software/chromedriver_selenium/chromedriver", chrome_options=options) # ?????? driver.set_page_load_timeout(self.TIMEOUT) driver.set_script_timeout(self.TIMEOUT) # ??????????? except Exception as e: self.error_logger.error("lxw get_chrome_driver() Exception: {0}\n{1}\n{2}\n\n".format(e, traceback.format_exc(), "--"*30)) if display: display.stop() if driver: driver.quit() return None, None else: return display, driver
def get_driver_chrome(self): # chromedriver options = webdriver.ChromeOptions() proxy = get_proxy() # NOTE: ??"http"?"https"??????????http?????https self.proxies["http"] = proxy self.proxies["https"] = proxy if proxy: options.add_argument('--proxy-server=' + proxy) display = Display(visible=0, size=(800, 800)) display.start() driver = webdriver.Chrome(executable_path=r"/home/lxw/Software/chromedriver_selenium/chromedriver", chrome_options=options) """ # PhantomJS: Not working. why? driver = webdriver.PhantomJS(executable_path=r"/home/lxw/Downloads/phantomjs/phantomjs-2.1.1-linux-x86_64/bin/phantomjs") proxy = webdriver.Proxy() proxy.proxy_type = ProxyType.MANUAL proxy_str = get_proxy() if proxy_str: proxy.http_proxy = proxy_str # ????????webdriver.DesiredCapabilities.PHANTOMJS? proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS) driver.start_session(webdriver.DesiredCapabilities.PHANTOMJS) """ # ?????? driver.set_page_load_timeout(self.TIMEOUT) driver.set_script_timeout(self.TIMEOUT) # ??????????? return driver
def get_driver_chrome(): # chromedriver options = webdriver.ChromeOptions() # display = Display(visible=1, size=(800, 800)) display = Display(visible=0, size=(800, 800)) display.start() driver = webdriver.Chrome(executable_path=r"/home/lxw/Software/chromedriver_selenium/chromedriver", chrome_options=options) # ?????? driver.set_page_load_timeout(60) driver.set_script_timeout(60) # ??????????? return display, driver
def setUpClass(cls): super(IntegrationTests, cls).setUpClass() # we do not display cls.display = Display(visible=0, size=(1024, 768)) cls.display.start() cls.selenium = WebDriver() # we create a user password = "password" cls.user = User(username="UserForLiveTests") cls.user.set_password(password) cls.user.save() # we log him in # source http://stackoverflow.com/questions/22494583/login-with-code-when-using-liveservertestcase-with-django # we need a session session = SessionStore() session[SESSION_KEY] = cls.user.id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = cls.user.get_session_auth_hash() session.save() # the cookie dict cls.cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'secure': False, 'path': '/', } # we launch centrifugo cls.centrifugo = subprocess.Popen(["centrifugo --config=tests/config.json --port={0}".format(getattr(settings, "CENTRIFUGO_PORT", 8802))], stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid) # we create participants cls.participant1 = Participant.objects.create(id=cls.user.id) cls.participant2 = Participant.objects.create(id=2) cls.participant3 = Participant.objects.create(id=3) cls.participant4 = Participant.objects.create(id=4) # we create a fake request cls.request = RequestFactory() cls.request.rest_messaging_participant = cls.participant1 # and wait for it to run time.sleep(4)
def get_web_screenshot(web_info): logger.info('Getting zapper web screenshot and uploading......\n') display = Display(visible=0) display.start() chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--no-sandbox') browser = webdriver.Chrome(executable_path=settings.CHROME_DRIVER_PATH, chrome_options=chrome_options) browser.set_window_size(web_info['width'], web_info['height']) browser.implicitly_wait(10) browser.get(web_info['url']) sleep(1) img_base64 = browser.get_screenshot_as_base64() browser.close() display.stop() # upload to imgur.com response = requests.request( "POST", url=IMGUR_API_URL, data=img_base64, headers={'authorization': 'Client-ID {client_id}'.format(client_id=settings.IMGUR_CLIENT_ID)} ) response_json = response.json() if response.status_code == 200: return response_json['data']['link'] else: logger.warning( ('ImgurApiError\n' 'Status Code: %s\n' 'Error Message: %s\n'), response.status_code, response_json['data']['error'] ) return False
def simulate_web(): display = Display(visible=0, size=(800, 600)) display.start() driver = webdriver.Chrome(cons.CHROME_PATH) return driver
def _screenshot_tweet(self): outfile = '%s/output/%s/screenshot.png' % (dir_path, self.tweet_id) if not os.path.isfile('%s/output/%s/screenshot.png' % (dir_path, self.tweet_id)): print(" fakepic: screenshotting tweet") if USE_VIRTUAL_DISPLAY: display = Display(visible=0, size=(1920, 600)) display.start() driver = webdriver.Firefox() driver.get(self.tweet_url) driver.save_screenshot(outfile) driver.quit()
def create_display(self): self.client_sys_info = platform.platform().lower() if self.client_sys_info.find("windows") == -1: self.display = Display(visible=0, size=(1280, 1024)) self.display.start() self.logger.info("display started.")
def setUpClass(cls): super(SplinterStaticLiveServerTestCase, cls).setUpClass() if cls.use_virtual_display: cls.virtual_display = Display(visible=0, size=cls.virtual_display_size) cls.virtual_display.start() cls.browser = Browser(cls.splinter_driver) cls.browser.driver.maximize_window()
def get_display(self): """ ?????????? """ if platform.system() != 'Darwin': # ??mac??, ???? display = Display(visible=0, size=(1024, 768)) display.start() else: display = None return display
def __init__(self): display = Display(visible=0, size=(1600, 1024)) display.start() self.driver = webdriver.Firefox() self.driver.delete_all_cookies()
def __init__(self, username=None, password=None, nogui=False): if nogui: self.display = Display(visible=0, size=(800, 600)) self.display.start() chrome_options = Options() chrome_options.add_argument('--dns-prefetch-disable') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--lang=en-US') chrome_options.add_experimental_option('prefs', {'intl.accept_languages': 'en-US'}) # managed_default_content_settings.images = 2: Disable images load, this setting can improve pageload & save bandwidth # default_content_setting_values.notifications = 2: Disable notifications # credentials_enable_service & password_manager_enabled = false: Ignore save password prompt from chrome chrome_prefs = { 'intl.accept_languages': 'en-US', 'profile.managed_default_content_settings.images': 2, 'profile.default_content_setting_values.notifications': 2, 'credentials_enable_service': False, 'profile': { 'password_manager_enabled': False } } chrome_options.add_experimental_option('prefs', chrome_prefs) self.browser = webdriver.Chrome('./assets/chromedriver', chrome_options=chrome_options) self.browser.implicitly_wait(5) self.logFile = open('./logs/logFile.txt', 'a') self.logFile.write('Session started - %s\n' \ % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) if not username or not password: print('Please provide Username and Password') return self.username = username self.password = password self.nogui = nogui self.followed = 0 self.ignore_users = [] self.aborting = False
def get_email_thread_content(ticket, emails): """ Generate `abuse.models.Ticket` emails thred history based on 'email_thread' `abuse.models.MailTemplate` :param `abuse.models.Ticket` ticket: The cererus ticket :param list emails: a list of `adapters.services.mailer.abstract.Email` :rtype: tuple :return: The content and the filetype """ try: template = MailTemplate.objects.get(codename='email_thread') is_html = '<html>' in template.body except ObjectDoesNotExist: raise EmailThreadTemplateNotFound('Unable to find email thread template') _emails = [] for email in emails: _emails.append(Email( sender=email.sender, subject=email.subject, recipient=email.recipient, body=email.body.replace('\n', '<br>') if is_html else email.body, created=datetime.fromtimestamp(email.created), category=None, attachments=None, )) domain = ticket.service.name if ticket.service else None try: template = loader.get_template_from_string(template.body) context = Context({ 'publicId': ticket.publicId, 'creationDate': ticket.creationDate, 'domain': domain, 'emails': _emails }) content = template.render(context) except (TemplateEncodingError, TemplateSyntaxError) as ex: raise EmailThreadTemplateSyntaxError(str(ex)) try: import pdfkit from pyvirtualdisplay import Display display = Display(visible=0, size=(1366, 768)) display.start() content = pdfkit.from_string(content, False) display.stop() return content, 'application/pdf' except: return content.encode('utf-8'), 'text/html' if is_html else 'text/plain'
def entry(arguments): """ The entry point for the dreammarket sink CLI interface. This defines the logic around the usage of command line arguments and the dreammarket sink in order to perform scraping, ingestion, and storage related functions. """ logger.setLevel(arguments.verbosity.upper()) if not arguments.dreammarket_username: logger.error("This sink requires a username to be specified through CLI or enviornment variable.") raise SystemExit() if not arguments.dreammarket_password: logger.error("This sink requires a password to be specified through CLI or environment variable.") raise SystemExit() if not arguments.dbc_access_key: logger.error("This sink requires a deathbycaptcha access key to be specified through CLI or environment variable.") raise SystemExit() if not arguments.dbc_secret_key: logger.error("This sink requires a deathbycaptcha secret key to be specified through CLI or environment variable.") raise SystemExit() display = Display(visible=0, size=(1366, 768)) display.start() sink = DreammarketSink( arguments.dreammarket_username, arguments.dreammarket_password, arguments.dbc_access_key, arguments.dbc_secret_key, url_file=arguments.url_file, save_to_directory=arguments.save_to_directory, onion_url=arguments.onion_url, request_interval=arguments.request_interval, request_retries=arguments.request_retries, request_timeout=arguments.request_timeout, category=arguments.category ) sink.logger = logger if arguments.ingest: if arguments.datastore == "stdout": store = STDOutInterface() parser = DreammarketParser(datastore=store) parser.parse(scrape_results=sink.scrape()) elif arguments.datastore == "elasticsearch": store = ElasticsearchInterface( host=arguments.datastore_host, port=arguments.datastore_port ) parser = DreammarketParser(datastore=store) parser.parse( scrape_results=sink.scrape( daemon=arguments.daemonize ) ) else: list(sink.scrape()) display.stop()
def firefox_driver(executable_path=None, run_headless=False, load_images=True, use_proxy=None): '''Function to initialize ``selenium.webdriver.Firefox`` with extended options Args: executable_path (str): path to the ``geckdriver`` binary. If set to ``None`` selenium will search for ``geckdriver`` in ``$PATH``. run_headless (bool): boolean flag that indicates if ``geckodriver`` has to be headless (without GUI). ``geckodriver`` doesn't support native headless mode, that's why ``pyvirtualdisplay`` is used. load_images (bool): boolean flag that indicates if Firefox has to render images. use_proxy (str): use http proxy in <host:port> format. Returns: selenium.webdriver.Firefox: created driver. Note: In order to create Firefox driver Selenium requires `Firefox <https://www.mozilla.org/en-US/firefox/new/>`_ to be installed and `geckodriver <https://github.com/mozilla/geckodriver/releases>`_ to be downloaded. Note: Firefox doesn't support native headless mode. We use ``pyvirtualdisplay`` to simulate it. In order ``pyvirtualdisplay`` to work you need to install ``Xvfb`` package: ``sudo apt install xvfb``. ''' firefox_profile = webdriver.FirefoxProfile() if run_headless: display = Display(visible=0, size=(1024, 768)) display.start() else: display = None if not load_images: firefox_profile.add_extension(os.path.dirname( os.path.realpath(__file__)) + '/browser_extensions/firefox/quickjava-2.1.2-fx.xpi') # Prevents loading the 'thank you for installing screen' firefox_profile.set_preference( 'thatoneguydotnet.QuickJava.curVersion', '2.1.2.1') # Turns images off firefox_profile.set_preference( 'thatoneguydotnet.QuickJava.startupStatus.Images', 2) # Turns animated images off firefox_profile.set_preference( 'thatoneguydotnet.QuickJava.startupStatus.AnimatedImage', 2) if use_proxy: _ = use_proxy.split(':') firefox_profile.set_preference('network.proxy.type', 1) firefox_profile.set_preference('network.proxy.http', _[0]) firefox_profile.set_preference('network.proxy.http_port', int(_[1])) firefox_profile.set_preference('network.proxy.ssl', _[0]) firefox_profile.set_preference('network.proxy.ssl_port', int(_[1])) if executable_path: driver = webdriver.Firefox( firefox_profile, executable_path=executable_path) else: driver = webdriver.Firefox(firefox_profile) driver.display = display return driver
def __init__(self): config = configparser.ConfigParser() config.read(os.getcwd() + '/' + 'config.ini') try: db = mysql.connector.connect( host=config['db']['host'], user=config['db']['user'], password=config['db']['pass'], database=config['db']['database'] ) db.autocommit = True except mysql.connector.errors.ProgrammingError as e: print " ERROR: Can`t connect to MySQL server! ({0})".format(str(e)) exit(0) try: mc = MongoClient(host=config['mongo']['host'], port=int(config['mongo']['port'])) mongo_collection = getattr(mc, config['mongo']['collection']) except pymongo.errors.ConnectionFailure as e: print " ERROR: Can`t connect to MongoDB server! ({0})".format(str(e)) exit(0) R = Registry() R.set('config', config) R.set('db', db) R.set('mongo', mongo_collection) R.set('wr_path', os.getcwd()) R.set('data_path', os.getcwd() + '/data/') R.set('http', Http()) R.set('ua', self.random_ua()) R.set('proxies', Proxies()) R.set( 'ndb', Database(config['db']['host'], config['db']['user'], config['db']['pass'], config['db']['database']) ) R.set( 'fuzzer_evil_value', file_get_contents(Registry().get('wr_path') + "/bases/fuzzer-evil-value.txt").strip() ) R.set('proxy_many_died', False) R.set('positive_limit_stop', False) if " ".join(sys.argv).count('selenium') and int(config['selenium']['virtual_display']): display = Display(visible=0, size=(800, 600)) display.start() R.set('display', display)