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

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

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

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

Q&A

解決済

1回答

854閲覧

clickがnullとか言われていますがどういう意味でしょう?

meshkit

総合スコア72

Node.js

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

0グッド

0クリップ

投稿2018/03/22 02:36

前提・実現したいこと

node.jsとpupetterでyoutubeで検索ボタンを押して画面をキャプチャーしています。
buttonを押すところでエラーになっています。
node.js、1日目なので、どう回避したものか困っています。
clickがnullとか言われていますがどういう意味でしょう?
ボタンを見つけられないのか、それともclickを間違えているのか。

回避策をいただければなおうれしいです。
//でコメントしたのはC#のコードと、try&errorのコードです。

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

Desktop\puppeteer-master\node_modules\puppeteer\examples> node.exe .\youtube.js (node:2224) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'click' of null (node:2224) [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. PS C:\Users\username\Desktop\puppeteer-master\node_modules\puppeteer\examples> node.exe .\youtube.js (node:13860) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'click' of null (node:13860) [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.

該当のソースコード

js

1'use strict'; 2 3const puppeteer = require('puppeteer'); 4 5(async() => { 6 const browser = await puppeteer.launch(); 7 const page = await browser.newPage(); 8 9 //(1)Process.Start("http://www.youtube.com/"); 10 await page.goto('http://www.youtube.com/'); 11 12 //(2)HtmlElement searchbox = webBrowser.Document.GetElementById("search-input"); 13 //searchbox.InnerText="検索キーワード"; 14 //<div id="search-input" slot="search-input"><input name="search_query" tabindex="0" class="ytd-searchbox" id="search" role="combobox" aria-haspopup="false" autofocus="" style="outline: invert;" dir="ltr" spellcheck="false" aria-autocomplete="list" aria-label="検索" type="text" placeholder="検索" autocomplete="off" autocorrect="off" autocapitalize="none"></div> 15 await page.type('#search-input', 'Lemon'); 16 //(3)HtmlElement searchbutton = webBrowser.Document.GetElementById("search-icon-legacy"); 17 //searchbutton.InvokeMember("click"); 18 19 const searchbutton = await page.$('search-icon-legacy'); 20 await searchbutton.click(); 21 22 // const searchbutton = 'search-icon-legacy'; 23 // await page.click(searchbutton); 24 25 26 await page.screenshot({path: 'example2.png'}); 27 await browser.close(); 28})(); 29

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

Windows10 64bit
node.js v8.9.4
pupetter

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

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

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

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

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

guest

回答1

0

ベストアンサー

page.$(selector)

selector <string> A selector to query page for
returns: <Promise<?ElementHandle>>
The method runs document.querySelector within the page. If no element matches the selector, the return value resolve to null.

https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageselector

const searchbutton = await page.$('search-icon-legacy'); await searchbutton.click();

searchbuttonがnullということは、つまり、ページ上にsearch-icon-legacyが存在しないのではないでしょうか?

search-icon-legacyはタグですか?もし、クラスの場合ならば、$('.search-icon-legacy')でしょうし、IDの場合は$('#search-icon-lagacy')になるでしょうし、その辺り、確認した方が良いかもしれません。

投稿2018/03/22 02:43

HayatoKamono

総合スコア2415

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

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

meshkit

2018/03/22 02:56

idでした。#でいけました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問