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

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

新規登録して質問してみよう
ただいま回答率
85.48%
VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

Chrome

Google Chromeは携帯、テレビ、デスクトップなどの様々なプラットフォームで利用できるウェブブラウザです。Googleが開発したもので、Blink (レンダリングエンジン) とアプリケーションフレームワークを使用しています。

Internet Explorer

Internet Explorer(IE;MSIE)はマイクロソフトが開発したウェブブラウザです。Microsoft Windowsに組み込まれています。

マクロ

定義された処理手続きに応じて、どのような一連の処理を行うのかを特定させるルールをマクロと呼びます。

Q&A

0回答

1372閲覧

【VBA】NoSuchElement Errorの原因がわかりません

m-nm0n

総合スコア15

VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

Chrome

Google Chromeは携帯、テレビ、デスクトップなどの様々なプラットフォームで利用できるウェブブラウザです。Googleが開発したもので、Blink (レンダリングエンジン) とアプリケーションフレームワークを使用しています。

Internet Explorer

Internet Explorer(IE;MSIE)はマイクロソフトが開発したウェブブラウザです。Microsoft Windowsに組み込まれています。

マクロ

定義された処理手続きに応じて、どのような一連の処理を行うのかを特定させるルールをマクロと呼びます。

0グッド

0クリップ

投稿2021/07/25 06:26

VBAで以下のプログラムを実行したとき、

VBA

1'待機 2#If VBA7 And Win64 Then 3Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr) '64ビットのエクセルで呼び出し 4#Else 5Private Declare Sub Sleep Lib "kernel32" (ByVal ms As Long) '32ビットのエクセルで呼び出し 6#End If 7 8 9Dim myBy As New By 10 11 12Sub mercari_auto_comment_tool() 13 14Dim time As Long 15 16'シートを移動 17 18Sheets("商品情報").Select 19 20If Cells(2, 1) = "" Then 21 22 MsgBox "入力してください。もしくは、入力内容に不備があります。 " 23 24Else 25 26 Sheets("商品情報").Select 27 28 'driver設置 29 30 Dim driver As New Selenium.ChromeDriver 31 32 driver.SetCapability "debuggerAddress", "127.0.0.1:9222" 33 34 driver.AddArgument "headless" 35 36 driver.Start "chrome" 37 38 39 'ループ処理 URL開く 40 41 Dim i As Integer 42 Dim tim As Integer 43 44 i = 2 45 tim = i 46 47 48 Do While tim = i 49 50 Sleep 500 51 52 'URLを開く 53 54 driver.Get Cells(i, 5) 55 56 57 'ループ処理 商品詳細開く 58 59 Dim product_counts As Integer 60 Dim product_variable As Integer 61 62 product_counts = 1 63 product_variable = product_counts 64 65 66 Do While product_counts = product_variable 67 68 Sleep 1000 69 70 Dim product_existence_css_judge As Boolean 71 72 product_existence_css_judge = driver.IsElementPresent(myBy.Css("body > div.default-container > main > div.l-content > section > div.items-box-content.clearfix > section:nth-child(" & product_counts & ") > a > figure > img")) 73 74 If product_existence_css_judge = False Then 75 76 product_variable = product_counts - 1 77 78 Else 79 80 81 '商品詳細開く 82 83 driver.FindElementByCss("body > div.default-container > main > div.l-content > section > div.items-box-content.clearfix > section:nth-child(" & product_counts & ") > a > figure > img").Click 84 85 86 'すでに自分のコメントがあるかで場合分け 87 88 89 90 Dim comment_css_judge As Boolean 91 92 comment_css_judge = driver.IsElementPresent(myBy.Css("body > div.default-container > div.item-detail-message > div > div:nth-child(1) > ul > li.clearfix.message-self > a > figure > figcaption")) 93 94 Sleep 1000 95 96 If comment_css_judge = True Then 97 98 driver.Get Cells(i, 5) '戻る 99 100 101 Else 102 103 'コメント送信 104 105 Dim comment_form_css_judge As Boolean 106 107 comment_form_css_judge = driver.IsElementPresent(myBy.Css("body > div.default-container > div.item-detail-message > div > div:nth-child(1) > ul > li.clearfix.message-self > a > figure > figcaption")) 108 109 If comment_form_css_judge = True Then 110 111 driver.FindElementByCss("body > div.default-container > div.item-detail-message > div > div:nth-child(2) > form > textarea").SendKeys Cells(i, 6) 112 driver.FindElementByCss("body > div.default-container > div.item-detail-message > div > div:nth-child(2) > form > button > span").Click 113 114 Else 115 116 driver.FindElementByCss("body > div.default-container > div.item-detail-message > div > div > form > textarea").SendKeys Cells(i, 6) 117 driver.FindElementByCss("body > div.default-container > div.item-detail-message > div > div > form > button > span").Click 118 119 120 End If 121 122 Sleep 500 123 124 driver.Get Cells(i, 5) '戻る 125 126 127 128 129 130 131 132 End If 133 134 135 136 137 138 139 '判定 140 141 product_counts = product_counts + 1 142 143 product_css_judge = driver.IsElementPresent(myBy.Css("body > div.default-container > main > div.l-content > section > div.items-box-content.clearfix > section:nth-child(" & product_counts & ") > a > figure > img")) 144 145 146 If product_css_judge = True Then 147 148 product_variable = product_counts 149 150 Else 151 152 product_variable = product_counts - 1 153 154 End If 155 156 157 158 End If 159 160 161 Loop 162 163 164 165 '判定 166 167 i = i + 1 168 169 If Cells(i, 1) = "" Then 170 171 tim = i - 1 172 173 Else 174 175 tim = i 176 177 End If 178 179 180 181 Loop 182 183 184 185 186 '終了 187 188 189 driver.Close '閉じる 190 191 192 193 MsgBox "一括コメントが完了しました" 194 195 196 197 198End If 199 200 201End Sub 202

たまに

実行時エラー"7" NoSuchElement Error "body > div.default-container > div.item-detail-message > div > div > form > textarea"

と出るときがあります。

以下のif構文でTrueになる条件のときに、
なぜかFalseのときにしか起こりえない

VBA

1"body > div.default-container > div.item-detail-message > div > div > form > textarea" 2という要素は存在しません

というエラーが出ます。

しかも、同じTrueになる条件のときでも、
このエラーが出ずにうまくいく場合もあれば、うまくいかない場合もあります。

ループ処理2回目、3回目でこの症状が出やすいです。
1回目でこのエラーが出たことはないです。

VBA

1 2 'すでに自分のコメントがあるかで場合分け 3 4 5 6 Dim comment_css_judge As Boolean 7 8 comment_css_judge = driver.IsElementPresent(myBy.Css("body > div.default-container > div.item-detail-message > div > div:nth-child(1) > ul > li.clearfix.message-self > a > figure > figcaption")) 9 10 Sleep 1000 11 12 If comment_css_judge = True Then 13 14 driver.Get Cells(i, 5) '戻る 15 16 17 Else 18 19 'コメント送信 20 21 Dim comment_form_css_judge As Boolean 22 23 comment_form_css_judge = driver.IsElementPresent(myBy.Css("body > div.default-container > div.item-detail-message > div > div:nth-child(1) > ul > li.clearfix.message-self > a > figure > figcaption")) 24 25 If comment_form_css_judge = True Then 26 27 driver.FindElementByCss("body > div.default-container > div.item-detail-message > div > div:nth-child(2) > form > textarea").SendKeys Cells(i, 6) 28 driver.FindElementByCss("body > div.default-container > div.item-detail-message > div > div:nth-child(2) > form > button > span").Click 29 30 Else 31 32 driver.FindElementByCss("body > div.default-container > div.item-detail-message > div > div > form > textarea").SendKeys Cells(i, 6) 33 driver.FindElementByCss("body > div.default-container > div.item-detail-message > div > div > form > button > span").Click 34 35 36 End If 37 38 Sleep 500 39 40 driver.Get Cells(i, 5) '戻る 41 42 43 44 45 46 47 48 End If

当方の力不足で原因がわかりませんでしたので、
わかる方おられましたら何卒ご教授いただけますと幸いです。

よろしくお願いいたします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問