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
当方の力不足で原因がわかりませんでしたので、
わかる方おられましたら何卒ご教授いただけますと幸いです。
よろしくお願いいたします。
あなたの回答
tips
プレビュー