質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

1回答

6924閲覧

Node.jsとpuppeteerで「現在のURL」を取得したいです。

meshkit

総合スコア72

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

0クリップ

投稿2018/03/23 01:47

編集2022/01/12 10:55

前提・実現したいこと

Node.jsとpuppeteerで「現在のURL」を取得したいです。
Node.js、2日目でまだ途方に暮れています。
よろしくお願いします。

発生している問題・エラーメッセージ

(node:7496) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'href' of undefined (node:7496) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

該当のソースコード

javascript

1'use strict'; 2 3const puppeteer = require('puppeteer'); 4 5(async() => { 6 const browser = await puppeteer.launch(); 7 const page = await browser.newPage(); 8 await page.goto('https://www.youtube.com/results?search_query='+ 'Lemon'); 9 console.log(page.location.href);

補足情報(FW/ツールのバージョンなど)

Windows 10
Node.js

https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageurl
を見たところ、page.url();でとれそうなのですが、これは最初に開いたurlで、現在のurlではありませんでした。取得したいのは、現在のurlです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Udomomo

2018/03/23 02:36

page.url();では具体的に何が返ってきましたか?
meshkit

2018/03/23 04:16

最初のURLです。
guest

回答1

0

page.goto(url, options)

url <string> URL to navigate page to. The url should include scheme, e.g. https://.
options <Object> Navigation parameters which might have the following properties:
timeout <number> Maximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the page.setDefaultNavigationTimeout(timeout) method.
waitUntil <string|Array<string>> When to consider navigation succeeded, defaults to load. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events can be either:
load - consider navigation to be finished when the load event is fired.
domcontentloaded - consider navigation to be finished when the DOMContentLoaded event is fired.
networkidle0 - consider navigation to be finished when there are no more than 0 network connections for at least 500 ms.
networkidle2 - consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.
returns: <Promise<?Response>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect.

https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options

const response = await page.goto('https://www.youtube.com/results?search_query='+ 'Lemon'); console.log(response)

page.gotoメソッドの戻り値の中身をまずは確認してみてはいかがでしょう?

投稿2018/03/23 02:51

編集2018/03/23 02:52
HayatoKamono

総合スコア2415

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meshkit

2018/03/23 07:45

Visual Studio Codeでデバッグしてみました。 URLは見当たらなかったのですが、どのあたりにありますか? pageのなかでしょうか。
HayatoKamono

2018/03/23 07:51 編集

私がコードを動かしている訳ではないので分かりません。 デバッグの結果を質問本文に追記しておくと、他の方からの回答もつきやすくなるかと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問