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 /類。
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 bracketawait 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 screenshotawait 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:
輸出圖像文件:
翻譯自: https://www.includehelp.com/node-js/interacting-with-forms-and-web-pages-using-node-js-and-puppeteer-1.aspx
puppeteer執行js
總結
以上是生活随笔為你收集整理的puppeteer执行js_使用Node.js和Puppeteer与表单和网页进行交互– 1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java类类getComponentTy
- 下一篇: 运算符sizeof_C程序通过使用siz