puppeteer执行js_使用Node.js和Puppeteer与表单和网页进行交互– 1

puppeteer执行js

Hi guys! Today let's look at another powerful function of the puppeteer API using Node.js.

嗨,大家好! 今天,让我们看看使用Node.js的puppeteer API的另一个强大功能。

In the first part of this section, let's look at commands for keystroke control and typing as we'll normally do with an opened browser.

在本节的第一部分中,让我们看一下用于击键控制和键入的命令,就像我们通常在打开的浏览器中所做的那样。

Isn't it cool!!??

是不是很酷!

Take Note! You should have Node.js and puppeteer installed in your PC.

做记录! 您应该在PC中安装Node.js和puppeteer。

With Node.js and puppeteer already up and running, let's get started.

现在已经启动并运行了Node.js和puppeteer,让我们开始吧。

*NB: If you don't yet have Node.js/ puppeteer installed in your PC, read the article NODE & GOOGLE PUPPETEER API SERIES

*注意:如果您的PC尚未安装Node.js / puppeteer,请阅读文章NODE&GOOGLE PUPPETEER API SERIES

Imagine we can write a code that opens a website, fills the form and submits it automatically.

想象一下,我们可以编写一个代码来打开一个网站,填写表格并自动提交。

Wow... Just using some few lines of code, everything is done for us like a robot...

哇...只需使用几行代码,一切就像机器人一样为我们完成了...

Hahahaa...

哈哈哈...

Indeed, the puppeteer has made work easy for you and me.

的确,木偶使您和我的工作变得轻松。

Let's see how opening a web page and typing is done.

让我们看看如何打开网页并输入内容。

Now, let's get started.

现在,让我们开始吧。

As usual, the page.goto command helps us access a webpage.

和往常一样,page.goto命令可帮助我们访问网页。

Now we'll make use of ; await page.keyboard.type ('ENGINEER GODWILL') and await page.focus('#example-1-email') used to type into the form and focus on the input element respectively.

现在我们将利用; await page.keyboard.type('ENGINEER GODWILL')await page.focus('#example-1-email')分别用于键入表单并专注于输入元素。

As we would normally do with an open browser, we first focus (or click) on the form field and then start typing..

就像通常使用打开的浏览器一样,我们首先将焦点放在(或单击)表单字段,然后开始键入。

Await.page.focus takes an obligatory parameter, which is the input element..

Await.page.focus具有一个强制性参数,它是输入元素。

To get the input element, right click on the field and click on inspect, a new window will pop up, then copy the id/ class of the element.

要获取输入元素,请右键单击该字段并单击检查,将弹出一个新窗口,然后复制该元素的ID /类。

Interacting with forms and web pages 1
Interacting with forms and web pages 2

Open a text editor and type the following code and save it with the file name app.js:

打开一个文本编辑器,输入以下代码,并将其保存为文件名app.js

The code below fills the form...

下面的代码填写表格...

const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('file:///H:/js-form-validator-master/index.html')
// focus on the element with id  specified in bracket
await page.focus('#example-1-name') 
// types the sentence in bracket 
await page.keyboard.type('ENGINEER GODWILL') 
await page.focus('#example-1-email') 
await page.keyboard.type('[email protected]')
await page.focus('#example-1-password')
await page.keyboard.type('admin123')
// captures a screenshot
await page.screenshot({ path: 'keyboard.png' }) 
console.log ('done');
await browser.close()
})()

NB: You can use any URL of your choice,

注意:您可以使用任何选择的URL,

I this example, I used a webpage that was already downloaded to facilitate the tutorial but if it concerns a URL on the www, internet connection is required to navigate to the website/web page.

在本示例中,我使用已经下载的网页来简化本教程,但是如果它涉及www上的URL,则需要互联网连接才能导航到该网站/网页。

** The hash symbol (#) is included because we used the id of the input element. If we were to use the class, we would use a dot.

**之所以包含井号(#),是因为我们使用了输入元素的ID。 如果要使用该类,则将使用点。

The file should be saved in your default Node.js directory.

该文件应保存在默认的Node.js目录中。

Run the code by initiating the file at the command prompt like a regular Node.js file.

通过在命令提示符处启动文件(如常规Node.js文件)来运行代码。

The Screenshot (image file) is then stored in the default Node.js directory with name keyboard.png

屏幕快照(图像文件)然后存储在名称为keyboard.png的默认Node.js目录中。

Output image file:

输出图像文件:

Interacting with forms and web pages 3

翻译自: https://www.includehelp.com/node-js/interacting-with-forms-and-web-pages-using-node-js-and-puppeteer-1.aspx

puppeteer执行js

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546109.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

zoj 1154 Niven numbers

见zoj 1154 还是需要将输入数据当作字符串来处理&#xff0c;不能直接使用整型。 /* zoj 1154 Niven numbers */#include <stdio.h> #define MAX 100 int isNivenNum(int base, char str[]);int main(void) {int totalBlocks;int base;int first 1;char str[MAX];scanf…

面试官:不会看SQL执行计划,简历也敢写精通SQL优化?

这是我的第 204 期分享作者 | 程序员内点事来源 | 程序员内点事&#xff08;ID&#xff1a;chengxy-nds&#xff09;分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;昨天中午在食堂&#xff0c;和部门的技术大牛们坐在一桌吃饭&#xff0c;作为一个卑微技…

scrollTop的兼容性小结

2019独角兽企业重金招聘Python工程师标准>>> 在页面上加上了 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 之后&#xff0c;document.body.scrollTop的值…

stl标准模板库_C ++标准模板库(STL)中的数组及其常用功能

stl标准模板库"array" is a container in C STL, which has fixed size, which is defined in "array" header. “ array”是C STL中的一个容器&#xff0c;具有固定大小&#xff0c;在“ array”标头中定义。 Declaration: 宣言&#xff1a; array <…

zoj 1074 To the MAX

见zoj 1074 参考了别人的思路才搞定。见http://blog.csdn.net/acm_davidcn/article/details/5834454 使用了最大连续子序列和的算法&#xff0c;虽然自己也知道这个算法&#xff0c;但是却没办法做到活学活用。 /* zoj 1074 To the Max */ #include <stdio.h> #inc…

阿里巴巴为什么让初始化集合时必须指定大小?

这是我的第 205 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;哈喽&#xff0c;亲爱的小伙伴们&#xff0c;技术学磊哥&#xff0c;进步没得说&#xff01;欢迎来到…

ios页面间跳转方式总结

转自&#xff1a;http://www.cnblogs.com/anywherego/p/3542202.html 下面以OldViewController(oldC)的按钮btn点击后跳转到NewViewController(newC)为例说明: 1.Storyboard的segues方式 鼠标点击按钮btn然后按住control键拖拽到newC页面&#xff0c;在弹出的segue页面中选择跳…

__asm___错误:“”前应有'=',',',',','asm'或'_attribute_'

__asm__A very common error in C programming language, it occurs when # is not used before the include. 这是C编程语言中非常常见的错误&#xff0c;当在include之前不使用&#xff03;时&#xff0c;就会发生此错误。 As we know that #include is a preprocessor dire…

Photoshop CS3 ICO 图标保存插件

最近编程发现&#xff0c;没有啥好看的图标文件于是&#xff0c;本人使用功能强大的ps&#xff0c;制作了图标文件做后发现&#xff0c;无法保存为ico图标文件在网上搜索了半天&#xff0c;终于从茫茫网海找到ico保存插件下载存放的地方是 PS根目录 即Adobe\Adobe Photoshop CS…

zoj 1005 jugs

题目内容见zoj1005 由于A&#xff0c;B互素且A的容量小于B&#xff0c;那么可以将B装满并且倒入A中&#xff0c;如果A被装满则将A中的内容全部清空&#xff0c;一直进行下去直到某一刻B中容量恰好等于目标的容量。这种方法能得到正确的结果&#xff0c;但是通常得不到最优结果…

啪啪打脸!领导说:try-catch要放在循环体外!

这是我的第 206 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;哈喽&#xff0c;亲爱的小伙伴们&#xff0c;技术学磊哥&#xff0c;进步没得说&#xff01;欢迎来到…

人类智商一般在多少左右?爱因斯坦的智商是多少?

智商 智商就是智力商数。智力通常叫智慧&#xff0c;也叫智能。是人们认识客观事物并运用知识解决实际问题的能力。智力包含多个方面&#xff0c;如观察力、记忆力、想象力、分析推断能力、思维能力、应变能力等。智力的高低通经常使用智力商数来表示&#xff0c;是用以标示智力…

软件项目与产品的区别与联系_软件产品和软件过程之间的区别和关系

软件项目与产品的区别与联系软件产品和软件过程 (Software product and Software process) Software product and Software process: These two words are the one which is mostly confused with each other. In this article, we are going to explain each of these in deta…

Oracle官方推荐的性能测试工具!简单、精准又直观!

这是我的第 207 期分享作者 | 武培轩来源 | 武培轩&#xff08;ID&#xff1a;wupeixuan404&#xff09;分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;磊哥在前面的所有性能优化的文章中都是用了这款性能测试工具——JMH&#xff0c;一直没来得及给大家…

系统美化 XP主题及其他

赢狗系统主题&#xff1a;下载马头桌名主题&#xff1a;下载windows美化大师&#xff1a;下载虽然电脑系统层出不穷但是仍对win xp有爱

zoj 3488 conic section

题目见zoj 3488 很简单的题目&#xff0c;却没能一次搞定&#xff0c;因为没看清楚题目中输入数据都是实数。 该题目考察浮点数的比较&#xff08;因为浮点数在计算机中存储是近似存储&#xff0c;所以不能直接将两个浮点数直接用大于或小于符号相比较&#xff09; /* zoj 3…

Hadoop开发第2期---虚拟机中搭建Linux

注&#xff1a;关于如何将hadoop源码导入Eclipse详见http://pan.baidu.com/s/1hq8ArUs 一、Hadoop配置软件&#xff08;我的电脑是Windows7旗舰--64bit&#xff09; 1. VMWare专用CentOS镜像(Centos是Linux操作系统的一种)2. VMware-workstation103. hadoop-1.1.2.tar.gz4. jdk…

c构造函数和析构函数_C ++构造函数,析构函数能力问题和答案(第2组)

c构造函数和析构函数C 构造函数和析构函数能力问题列表 (List of C Constructor and Destructor Aptitude Questions & Answers) 1) Constructor(s) which is/are added automatically with a class, if we do not create our own constructor? 1)如果我们不创建自己的构造…

看故事学知识,这篇Java代理的文章妙啊!

这是我的第 208 期分享作者 | java金融来源 | java金融&#xff08;ID&#xff1a;java4299&#xff09;分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;什么是代理代理模式是常用的java设计模式&#xff0c;他的特征是代理类与委托类有同样的接口&#x…

[下载]青岛交通旅游地图[download]

清闲无事 所以就把青岛地图扫描了一下有需要青岛旅游地图的可以下载the download 【下载】