iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python中ghost的使用
  • 956
分享到

Python中ghost的使用

Pythonghost 2023-01-31 02:01:46 956人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

ghost.py is a WEBkit web client written in python. from ghost import Ghostghost = Ghost()page, extra_resources = ghos

ghost.py is a WEBkit web client written in python.

from ghost import Ghostghost = Ghost()page, extra_resources = ghost.open("Http://jeanphi.fr")assert page.http_status==200 and 'jeanphix' in ghost.content

First you need to install either PyQt or PySide that are availables for many platfORMs.

Then you may install ghost.py using pip:

pip install Ghost.py

First of all, you need a instance of Ghost web client:

from ghost import Ghostghost = Ghost()

Ghost provide a method that open web page the following way:

page, resources = ghost.open('http://my.web.page')

This method returns a tuple of main resource (web page) and all loaded resources (such as CSS files, javascripts, images...).

All those resources are backed as HttpResource objects.

At the moment Httpresource objects provide the following attributes:

  • url: The resource url.
  • http_status: The HTTP response status code.
  • headers: The response headers as a dict.

Executing javascripts inside webkit frame is one of the most interesting features provided by Ghost:

result, resources = ghost.evaluate( "document.getElementById('my-input').getAttribute('value');")

The return value is a tuple of:

  • last javascript last statement result.
  • loaded resources (e.g.: when an XHR is fired up).

As many other Ghost methods, you can pass an extra parameter that tells Ghost you expect a page loading:

page, resources = ghost.evaluate( "document.getElementById('link').click();", expect_loading=True)

Then the result tuple wil be the same as the one returned by Ghost.open().

Fill a field

You can set a form field value trougth Ghost.set_field_value(selector, value, blur=True, expect_loading=False):

result, resources = ghost.set_field_value("input[name=username]", "jeanphix")

If you set optional parameter `blur` to False, the focus will be left on the field (usefull for autocomplete tests).

For filling file input field, simply pass file path as `value`.

Fill an entire form

You can fill entire form trougth Ghost.fill(selector, values, expect_loading=False):

result, resources = ghost.fill("form", { "username": "jeanphix", "passWord": "mypassword"})

Submit the form

Yon can submit the form by firing `submit` event:

page, resources = ghost.fire_on("form", "submit", expect_loading=True)

Ghost provides several methods for waiting for specific things before the script continue execution:

wait_for_alert()

That wait until a javascript alert() is send.

result, resources = ghost.wait_for_alert()

wait_for_page_loaded()

That wait until a new page is loaded.

page, resources = ghost.wait_for_page_loaded()

wait_for_selector(selector)

That wait until a element match the given selector.

result, resources = ghost.wait_for_selector("ul.results")

wait_for_text(text)

That wait until the given text exists inside the frame.

result, resources = ghost.wait_for_selector("My result")

Accept or deny javascript confirm is quite easy:

with Ghost.confirm(): # The confirm() box fired up by click will be accepted self.ghost.click('#confirm-button')with Ghost.confirm(False): # The confirm() box fired up by click will be denied self.ghost.click('#confirm-button')

Filling a value in prompt box:

with Ghost.prompt('my value'): # prompt() box fired up by click will be filled with 'my value' self.ghost.click('#prompt-button')

Ghost.capture_to(path, region=None, selector=None) method let's you take webkit current frame screenshots.

If you need to capture a specific region of the viewport, just provide a selector (or coordinates tuple) that feets your needs:

ghost.capture_to('header.png', selector="header")

Requirements:

pip install tornado

ghost.py provides a simple GhostTestCase that deals with WSGI applications:

import unittestfrom flask import Flaskfrom ghost import GhostTestCaseapp = Flask(__name__)@app.route('/')def home(): return 'hello world'class MyTest(GhostTestCase): port = 5000 @claSSMethod def create_app(cls): return app def test_open_home(self): self.ghost.open("http://localhost:%s/" % self.port) self.assertEqual(self.ghost.content, 'hello world')if __name__ == '__main__': unittest.main()

Debug your test

Tests can be run with a UI by setting display class member to True:

class MyTest(GhostTestCase): display = True

The following test tries to center http://www.openstreetmap.org/ map to France:

# Opens the web pageghost.open('http://www.openstreetmap.org/')# Waits for form search fieldghost.wait_for_selector('input[name=query]')# Fills the formghost.fill("#search_form", {'query': 'France'})# Submits the formghost.fire_on("#search_form", "submit")# Waits for results (an XHR has been called here)ghost.wait_for_selector( '#search_osm_nominatim .search_results_entry a')# Clicks first result linkghost.click( '#search_osm_nominatim .search_results_entry:first-child a')# Checks if map has moved to expected latitudelat, resources = ghost.evaluate("map.center.lat")assert float(lat.toString()) == 5860090.806537

Ghost provides an extension for Django built on to of the upcoming LiveServerTestCase.

from ghost.ext.djanGo.test import GhostTestCase

--结束END--

本文标题: Python中ghost的使用

本文链接: https://www.lsjlt.com/news/187031.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python中ghost的使用
    ghost.py is a webkit web client written in python. from ghost import Ghostghost = Ghost()page, extra_resources = ghos...
    99+
    2023-01-31
    Python ghost
  • GHOST使用教程图解
    Ghost使用教程 一、什么是Ghost?  Ghost(幽灵)软件是美国赛门铁克公司推出的一款出色的硬盘备份还原工具,可以实现FAT16、FAT32、NTFS、OS2等多种硬盘分区格式的分区及硬盘的备份还原。俗称克隆软...
    99+
    2023-05-23
    GHOST
  • Linux系统ghost怎么使用
    这篇文章跟大家分析一下“Linux系统ghost怎么使用”。内容详细易懂,对“Linux系统ghost怎么使用”感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面跟着小编一起深入学习“Linux系统ghos...
    99+
    2023-06-28
  • win10如何使用一键ghost
    本文小编为大家详细介绍“win10如何使用一键ghost”,内容详细,步骤清晰,细节处理妥当,希望这篇“win10如何使用一键ghost”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。win10使用一键ghost的...
    99+
    2023-07-01
  • linux中能不能用ghost
    本文小编为大家详细介绍“linux中能不能用ghost”,内容详细,步骤清晰,细节处理妥当,希望这篇“linux中能不能用ghost”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。linux中能使用ghost;gh...
    99+
    2023-07-02
  • 电脑中如何使用一键GHOST备份系统
    这篇文章将为大家详细讲解有关电脑中如何使用一键GHOST备份系统,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一键ghost备份系统的具体操作如下:首先下载并运行“一键GHOST硬盘版”工具,在软件的主界...
    99+
    2023-06-27
  • windows ghost一键安装怎么使用
    本篇内容介绍了“windows ghost一键安装怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!ghost一键安装使用方法:首先进入...
    99+
    2023-07-01
  • 教你使用ghost一键装机系统教程
    重装系统的方法很多,除了常用的一键重装系统,启动盘重装系统的方法之外,还有网友习惯自行下载系统镜像文件或者备份系统后采用ghost一键装机的方法来安装系统。那么一键ghost如何使用呢?下面小编就给大家演示下使用ghost一键装机系统教程。...
    99+
    2023-07-12
  • win10应用一键ghost的流程
    win10如何使用一键ghost系统软件许多安装了ghostwin10系统的小伙伴们不清楚该怎么使用一键ghost,实际上ghostwin10是一款非常好的系统软件,十分的平稳便捷,今日我产生了详尽的安装方法。实际的一起来看一下win10应...
    99+
    2023-07-15
  • Windows7中不需要用Ghost来做备份
    Windows 7(以下简称Win 7)正式版终于发布了,在我们体验它带来的诸多改进之前,有一件事是必须做的,那就是系统的备份工作。在Vista时代,Windows的备份还原功能还非常羸弱,那么在Win 7当中,这一功能...
    99+
    2023-05-25
    Windows7 Ghost 备份
  • Python中的*使用
    Python中的*使用   在为函数传递参数和函数定义时使用参数的时候,时常会看到有和 *和**,下面分别讲解其作用。 调用函数时使用*和 ** 假设有函数 def test(a, b, c) test(*args):* 的作...
    99+
    2023-01-31
    Python
  • 如何使用GHOST 方式来安装XP 和Win7 双系统?
    答:要想在XP 的基本上安装XP/Win7的双系统,建议你照旧运用正常方式安装的Win7 光盘来安装,这样要简单得多,在安装完成后就自动会呈现多系统启动挑选菜单了。假设你照旧想持续运用GHOST方式安装Win7 的话,本...
    99+
    2023-05-25
    GHOST XP Win7 和Win7 双系统
  • Fedora vmware如何启动ghost computer中的Linux
    本篇内容主要讲解“Fedora vmware如何启动ghost computer中的Linux”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Fedora vmware如何启动ghost comp...
    99+
    2023-06-16
  • python 中conda的使用
    anaconda中conda可以代替pip,比如安装包,还可以定制环境。 1,安装package ​conda install jupyter notebook 2,​Conda 定制环境 创建python2.7环境 conda creat...
    99+
    2023-01-31
    python conda
  • Python中celery的使用
    目录 Celery简介celery的异步任务1.安装celery2.安装redis3.使用ceelryDjango中使用celery1.创建celery文件2.添加cele...
    99+
    2024-04-02
  • python中list的使用
    1、list(列表)是一种有序的集合,可以随时添加、修改、删除其中的元素。 举例:listClassName = ['Jack','Tom','Mark']                     列表可以根据索引获取元素,如:listC...
    99+
    2023-01-30
    python list
  • python中assert的使用
       在python程序中,如果想要确保程序中的某个条件一定为真才会继续执行的话,而可以使用assert来实现。  例如:>>> age = 10 >>> assert 0<age<20 &...
    99+
    2023-01-31
    python assert
  • 无法使用GHOST及避免出现A:\GHOSTERR.TXT怎么办
    这篇文章主要为大家展示了“无法使用GHOST及避免出现A:\GHOSTERR.TXT怎么办”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“无法使用GHOST及避免出现A:\GHOSTERR.TXT...
    99+
    2023-06-09
  • python中列表的使用
    目的:熟练使用列表函数,方便管理多个变量值环境:ubuntu 16.04  python 3.5.2情景:列表应该是数据处理时经常使用到一种数据类型,可以有序、组合的操作值存储,是很实用的函数。。。这是最后一篇整理的笔记,发现排版很浪费时间...
    99+
    2023-01-31
    列表 python
  • python中Scrapy shell的使用
    前言: 我们想要在爬虫中使用xpath、beautifulsoup、正则表达式,css选择器等来提取想要的数据,但是因为scrapy是一个比较重的框架,每次运行都要等到一段时间,因此...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作