I created a VBA macro that uses selenium to go to a website and pastes inputs in multiple fields and uploads some files by copying the file paths from the excel sheet. It has a for loop so it needs to do this thousands of times.
I used it on one of my computers and it worked perfectly for the first 20 times or so before it started to paste “echo is on” for one of the file paths instead of the file path on one of the iterations of the loop. Oddly, even on that iteration of the loop it pastes multiple other values successfully before doing this.
I tried the identical code on the same spreadsheet on a different computer and the problem seemed to disappear. I ran the macro for another 1,000 iterations and it worked flawlessly but then still started pasting the “echo is on” again for that same filepath cell.
Here is my code if that helps
Option Explicit
Dim MyBrowser As New Selenium.ChromeDriver
Sub SubmitTheHungryJPEGLoop()
Dim i
Dim strFilePath
Dim firstimageupload
Dim zipupload
Dim WshShell
Dim secondimageupload
Dim PinterestPin
Dim AddTitle
Dim AddDescription
Dim SubmissionCategory
Set MyBrowser = New Selenium.ChromeDriver
Dim iframeelement As WebElement
Dim noError As Boolean ' Define noError as a boolean variable
MyBrowser.Start baseUrl:="example.com"
MyBrowser.Get "https://example.com"
Application.Wait DateAdd("s", 35, Now)
MyBrowser.FindElementByXPath("/html/body/div[1]/header/div[1]/div[2]/div/div[2]/ul/li[1]/a[1]").Click
Application.Wait DateAdd("s", 3, Now)
MyBrowser.FindElementByXPath("/html/body/div[2]/div/div/div[1]/form/div[4]/input").SendKeys "myloginemail"
MyBrowser.FindElementByXPath("/html/body/div[2]/div/div/div[1]/form/div[5]/input").SendKeys "mypassword"
MyBrowser.FindElementByXPath("/html/body/div[2]/div/div/div[1]/form/div[7]/button").Click
For i = 4 To 10
linek:
noError = True
On Error GoTo linej:
AddTitle = Sheet1.Cells(i, 1).Value
AddDescription = Sheet1.Cells(i, 4).Value
firstimageupload = Sheet1.Cells(i, 2).Value
PinterestPin = Sheet1.Cells(i, 29).Value
SubmissionCategory = Sheet1.Cells(i, 22).Value
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo " & firstimageupload & "| clip", 0, True
Application.Wait DateAdd("s", 15, Now)
MyBrowser.Get "https://thehungryjpeg.com/add_product"
Application.Wait DateAdd("s", 20, Now)
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[1]/div/div[1]/ul/li/div/div[2]/input").ClickAndHold
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[1]/div/div[1]/ul/li/div/div[2]").Click
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{v}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{ENTER}"
Application.Wait DateAdd("s", 2, Now)
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[1]/input").SendKeys AddTitle
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[2]/div/div[1]/select").Click
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{ENTER}"
If SubmissionCategory = "Shapes" Then
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[2]/div/div[2]/select").Click
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{ENTER}"
End If
If SubmissionCategory = "Quotes" Then
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[2]/div/div[2]/select").Click
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{ENTER}"
End If
If SubmissionCategory = "Animals" Then
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[2]/div/div[2]/select").Click
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{ENTER}"
End If
If SubmissionCategory = "Sublimation" Then
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[2]/div/div[2]/select").Click
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{ENTER}"
End If
If SubmissionCategory = "Travel" Then
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[2]/div/div[2]/select").Click
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{ENTER}"
End If
If SubmissionCategory = "Holiday" Then
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[2]/div/div[2]/select").Click
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{ENTER}"
End If
If SubmissionCategory = "People" Then
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[2]/div/div[2]/select").Click
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{DOWN}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{ENTER}"
End If
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[6]/div/div[14]/input").Click
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[6]/div/div[13]/input").Click
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[6]/div/div[4]/input").Click
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[6]/div/div[10]/input").Click
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[3]/div/input").SendKeys "2"
zipupload = Sheet1.Cells(i, 3).Value
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[7]/div/div/input").Click
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[2]/div/div[9]/div").Click
WshShell.Run "cmd.exe /c echo " & zipupload & "| clip", 0, True
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{v}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{ENTER}"
Application.Wait DateAdd("s", 2, Now)
Set iframeelement = MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[1]/div[1]/div/div[2]/div[1]/div/div/iframe")
MyBrowser.SwitchToFrame iframeelement
MyBrowser.FindElementByXPath("/html/body").SendKeys AddDescription
MyBrowser.SwitchToParentFrame
Application.Wait DateAdd("s", 4, Now)
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[2]/button[2]").Click
MyBrowser.FindElementByXPath("/html/body/div[1]/div/section/form/div/div[2]/button[2]").Click
WshShell.Run "cmd.exe /c echo " & firstimageupload & "| clip", 0, True
If noError Then ' Check if no error occurred
GoTo linel:
End If
linej:
On Error GoTo -1
WshShell.SendKeys "^{ENTER}"
Application.Wait DateAdd("s", 2, Now)
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{ENTER}"
Application.Wait DateAdd("s", 2, Now)
Application.Wait DateAdd("s", 10, Now)
WshShell.SendKeys "{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "^{TAB}"
Application.Wait DateAdd("s", 2, Now)
WshShell.SendKeys "{ENTER}"
GoTo linek:
linel:
Next
End Sub
Does anyone have any thoughts on how to fix this? I did look at the posts under the “Review questions already on Stack Overflow to see if your question is a duplicate.” section to see if there was a solution, but those don’t seem to be about VBA and don’t talk about a situation where the program works for a while and then stops for some reason.
I tried testing on different computers and that worked for a bit. I also tried moving the section of the code higher up to see if that changed anything but it didn’t.