Vba Selenium problems "no such element error"

JasonLim

New Member
Joined
Dec 23, 2021
Messages
31
Office Version
  1. 365
Platform
  1. Windows
I had this pop out box stated with "no such element error". I really don't know what's wrong with the code. This basic code is actually looking up for the search box and input text which is ABC. I hope I can find someone who can kindly guide me. Your help will be very much appreciated. TQ

Set mybrowser = New Selenium.ChromeDriver

sel.Start "chrome", "Carousell Singapore | Buy & Sell Goods, Cars, Services and Property"
sel.Get "/"
'Stop

sel.FindElementByCss("a.D_pG.D_pH.D_pT.D_mz").SendKeys "ABC"
 

Attachments

  • Screenshot 2022-06-27 220141.jpg
    Screenshot 2022-06-27 220141.jpg
    221.7 KB · Views: 436
Wow, it works well everyday. You are the pro of the pro. : ) Thank you so much. But this code you provide is something I will never understand how you did that. : (
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Thank you for the feedback
If you deem that the problem has been resolved then it would be better if you mark the discussion as solved; see Mark as Solution
 
Upvote 0
Dear Sir, so sorry to trouble you again. I had tried more than a week to extract the information like description, price and the Url link website of each item. But I really can't find any way to do it, do you mind giving me a helping hand? : )


Sub AlwayUseTheToTest()

Dim WPage As Selenium.WebDriver, myUrl As String
Dim tObj As Object, myTim As Single, AColl As Object, BColl As Object
Dim noBB As Boolean, MI As Long
'
myTim = Timer
'Crea Driver:
' Set WPage = CreateObject("Selenium.EdgeDriver")
Set WPage = CreateObject("Selenium.CHRomedriver")
'
myUrl = "toys - View all toys ads in Carousell Singapore" '<<< YOUR Url
'
WPage.Get myUrl
WPage.Wait 500
Debug.Print "Page loaded", Format(Timer - myTim, "0.0")
'
'Search the menu container:
Set AColl = WPage.FindElementsByTag("div")

Debug.Print AColl.Count

For I = 1 To AColl.Count
If InStr(1, AColl(I).Attribute("style"), "top:", vbTextCompare) = 1 Then
Debug.Print "Found menus DIV, # " & I
Exit For
End If
Next I
MI = I
'...and click the second one:
Set tObj = AColl(MI).FindElementsByTag("svg")



tObj(2).FindElementByXPath("./..").Click
WPage.Wait 500
'Search the RadioButtons Container:
Set BColl = WPage.FindElementsByTag("div")
For I = 1 To BColl.Count
If InStr(1, BColl(I).Attribute("style"), "left:", vbTextCompare) = 1 Then
Debug.Print "Found RadioButtons DIV, # " & I
Exit For

End If

Next I
'...and click the second one:
Set tObj = BColl(I).FindElementsByTag("input")
tObj(2).Click
WPage.Wait 500
Debug.Print "YOU", AColl(MI).Text
If InStr(1, AColl(MI).Text, "Recent", vbTextCompare) > 0 Then
Debug.Print "Recent has been selected"
noBB = False
Else
noBB = True
End If
Debug.Print "noBB=" & noBB
If noBB Then
AppActivate (Application.Caption)
MsgBox ("Selecting Recent failed; select it manually before closing the msgbox")
''Stop '<<< This is only for test
End If




End Sub
 
Upvote 0
These jobs are just a matter of time and patience, and typical what you (me) learn will not be useful for the next assignment.
Also, the forum should show you how to solve the problems, not (only) solve the problems
This to say that I shall try to answer your latest problem, but I woud like then to forget about carousell.sg

So, after having selected "Recent" you would like to import description, price, hyperlink to the item of the listed products...
This can be obtained, today, with the following code:
Code:
Sub Corousell4()
'per JasonLim - MrEx
'Vedi https://www.mrexcel.com/board/threads/vba-selenium-problems-no-such-element-error.1209120/#post-5918264
Dim WPage As Selenium.WebDriver, myUrl As String
Dim tObj As Object, myTim As Single, AColl As Object, BColl As Object
Dim noBB As Boolean, MI As Long
Dim NextR As Long, cCnt As Long
'
myTim = Timer
'Crea Driver:
'    Set WPage = CreateObject("Selenium.EdgeDriver")
    Set WPage = CreateObject("Selenium.CHRomedriver")
'
myUrl = "https://www.carousell.sg/search/toys?SearchId=GL7fvY&addrecent=true&canChangeKeyword=true&includeSuggestions=true&searchId=3XYjuS"    'Your Url
Sheets("Foglio1").Select                    '<<< The sheets that will be used
'
WPage.Get myUrl
WPage.Wait 500
Debug.Print ">Page loaded", Format(Timer - myTim, "0.0")
'
'Search the menu container:
Set AColl = WPage.FindElementsByTag("div")
For I = 1 To AColl.Count
    If InStr(1, AColl(I).Attribute("style"), "top:", vbTextCompare) = 1 Then
        Debug.Print ">Found menus DIV # " & I
        Exit For
    End If
Next I
MI = I
'...and click the second one:
Set tObj = AColl(MI).FindElementsByTag("svg")
tObj(2).FindElementByXPath("./..").Click
WPage.Wait 500
'Search the RadioButtons Container:
Set BColl = WPage.FindElementsByTag("div")
For I = 1 To BColl.Count
    If InStr(1, BColl(I).Attribute("style"), "left:", vbTextCompare) = 1 Then
        Debug.Print ">Found RadioButtons DIV # " & I
        Exit For
    End If
Next I
'...and click the second one:
Set tObj = BColl(I).FindElementsByTag("input")
tObj(2).Click
WPage.Wait 500
Debug.Print ">ZxZx", AColl(MI).Text
If InStr(1, AColl(MI).Text, "Recent", vbTextCompare) > 0 Then
    Debug.Print ">Recent has been selected"
    noBB = False
Else
    noBB = True
End If
Debug.Print ">noBB=" & noBB
If noBB Then
    AppActivate (Application.Caption)
    MsgBox ("Selecting Recent failed; select it manually before closing the msgbox")
    ''Stop            '<<< This is only for test
End If
WPage.Wait 500
'Look for the Products:
Debug.Print ">Let's search the Products", Format(Timer - myTim, "0.0")
'Scroll to end of page..
WPage.ExecuteScript ("window.scrollTo(0, document.body.scrollHeight);")
WPage.Wait 1500
Set tObj = WPage.FindElementsByTag("Main")(1)
Set AColl = tObj.FindElementsByTag("div")
Debug.Print ">AColl.Count=" & AColl.Count
NextR = Cells(Rows.Count, 1).End(xlUp).Row + 1
For I = 1 To AColl.Count
    If InStr(1, AColl(I).Attribute("data-testid"), "listing-card", vbTextCompare) > 0 Then
        NextR = NextR + 1
        Debug.Print "Row " & NextR, "AColl Index=" & I, Format(Timer - myTim, "0.0")
        Cells(NextR, 1).Resize(1, 3).Clear
        Set BColl = AColl(I).FindElementsByTag("div")(1).FindElementsByTag("a")
        cCnt = BColl(2).FindElementsByTag("p").Count
        If cCnt > 2 Then
            Cells(NextR, 1) = BColl(2).FindElementsByTag("p")(cCnt - 3).Text & " "
            Cells(NextR, 2) = BColl(2).FindElementsByTag("p")(cCnt - 2).Text
            If BColl.Count > 1 Then
                ActiveSheet.Hyperlinks.Add anchor:=Cells(NextR, 1), _
                   Address:=BColl(2).Attribute("href")
            End If
        End If
    End If
Next I
Debug.Print ">Completed"
End Sub
There is one line marked <<<, at the beginning of the macro, that need to be customized with your data

Try...
 
Upvote 0
Hi, bro, your code works well when I search for things like cars or toys, but I encounter problems when I search things like battery,phone or shoes "Index was outside the bounds of the array" appear.
 
Upvote 0
Hi, Bro, I happened to find a way to solve the issue I had msg you yesterday. Thanks for helping me so much. Your help will be very much appreciated. Thank you.
 
Upvote 0
These jobs are just a matter of time and patience, and typical what you (me) learn will not be useful for the next assignment.
Also, the forum should show you how to solve the problems, not (only) solve the problems
This to say that I shall try to answer your latest problem, but I woud like then to forget about carousell.sg

So, after having selected "Recent" you would like to import description, price, hyperlink to the item of the listed products...
This can be obtained, today, with the following code:
Code:
Sub Corousell4()
'per JasonLim - MrEx
'Vedi https://www.mrexcel.com/board/threads/vba-selenium-problems-no-such-element-error.1209120/#post-5918264
Dim WPage As Selenium.WebDriver, myUrl As String
Dim tObj As Object, myTim As Single, AColl As Object, BColl As Object
Dim noBB As Boolean, MI As Long
Dim NextR As Long, cCnt As Long
'
myTim = Timer
'Crea Driver:
'    Set WPage = CreateObject("Selenium.EdgeDriver")
    Set WPage = CreateObject("Selenium.CHRomedriver")
'
myUrl = "https://www.carousell.sg/search/toys?SearchId=GL7fvY&addrecent=true&canChangeKeyword=true&includeSuggestions=true&searchId=3XYjuS"    'Your Url
Sheets("Foglio1").Select                    '<<< The sheets that will be used
'
WPage.Get myUrl
WPage.Wait 500
Debug.Print ">Page loaded", Format(Timer - myTim, "0.0")
'
'Search the menu container:
Set AColl = WPage.FindElementsByTag("div")
For I = 1 To AColl.Count
    If InStr(1, AColl(I).Attribute("style"), "top:", vbTextCompare) = 1 Then
        Debug.Print ">Found menus DIV # " & I
        Exit For
    End If
Next I
MI = I
'...and click the second one:
Set tObj = AColl(MI).FindElementsByTag("svg")
tObj(2).FindElementByXPath("./..").Click
WPage.Wait 500
'Search the RadioButtons Container:
Set BColl = WPage.FindElementsByTag("div")
For I = 1 To BColl.Count
    If InStr(1, BColl(I).Attribute("style"), "left:", vbTextCompare) = 1 Then
        Debug.Print ">Found RadioButtons DIV # " & I
        Exit For
    End If
Next I
'...and click the second one:
Set tObj = BColl(I).FindElementsByTag("input")
tObj(2).Click
WPage.Wait 500
Debug.Print ">ZxZx", AColl(MI).Text
If InStr(1, AColl(MI).Text, "Recent", vbTextCompare) > 0 Then
    Debug.Print ">Recent has been selected"
    noBB = False
Else
    noBB = True
End If
Debug.Print ">noBB=" & noBB
If noBB Then
    AppActivate (Application.Caption)
    MsgBox ("Selecting Recent failed; select it manually before closing the msgbox")
    ''Stop            '<<< This is only for test
End If
WPage.Wait 500
'Look for the Products:
Debug.Print ">Let's search the Products", Format(Timer - myTim, "0.0")
'Scroll to end of page..
WPage.ExecuteScript ("window.scrollTo(0, document.body.scrollHeight);")
WPage.Wait 1500
Set tObj = WPage.FindElementsByTag("Main")(1)
Set AColl = tObj.FindElementsByTag("div")
Debug.Print ">AColl.Count=" & AColl.Count
NextR = Cells(Rows.Count, 1).End(xlUp).Row + 1
For I = 1 To AColl.Count
    If InStr(1, AColl(I).Attribute("data-testid"), "listing-card", vbTextCompare) > 0 Then
        NextR = NextR + 1
        Debug.Print "Row " & NextR, "AColl Index=" & I, Format(Timer - myTim, "0.0")
        Cells(NextR, 1).Resize(1, 3).Clear
        Set BColl = AColl(I).FindElementsByTag("div")(1).FindElementsByTag("a")
        cCnt = BColl(2).FindElementsByTag("p").Count
        If cCnt > 2 Then
            Cells(NextR, 1) = BColl(2).FindElementsByTag("p")(cCnt - 3).Text & " "
            Cells(NextR, 2) = BColl(2).FindElementsByTag("p")(cCnt - 2).Text
            If BColl.Count > 1 Then
                ActiveSheet.Hyperlinks.Add anchor:=Cells(NextR, 1), _
                   Address:=BColl(2).Attribute("href")
            End If
        End If
    End If
Next I
Debug.Print ">Completed"
End Sub
There is one line marked <<<, at the beginning of the macro, that need to be customized with your data

Try...
Hi, Bro, how are you? I hope you still remember me, I had been using the code that you had provided me, it works amazingly, But a few days ago I encountered an error, stating on the pop out box "Index is outside bounds of the array" I had tried a few days, but still couldn't find any solutions. I seek your kind assistance. TQ
 
Upvote 0
As I said, "These jobs are just a matter of time and patience"; and (I should have added) they have an expiration date. Good (for you) that the previous version lasted several months, till the site owner decides to change some commas here and there.

This is my revised code:
Code:
Sub Corousell5()
'per JasonLim - MrEx
'Vedi https://www.mrexcel.com/board/threads/vba-selenium-problems-no-such-element-error.1209120/#post-5918264
Dim WPage As Selenium.WebDriver, myUrl As String
Dim tObj As Object, myTim As Single, AColl As Object, BColl As Object
Dim noBB As Boolean, MI As Long
Dim NextR As Long, cCnt As Long
'
myTim = Timer
'Crea Driver:
'    Set WPage = CreateObject("Selenium.EdgeDriver")
    Set WPage = CreateObject("Selenium.CHRomedriver")
'
myUrl = "https://www.carousell.sg/search/toys?SearchId=GL7fvY&addrecent=true&canChangeKeyword=true&includeSuggestions=true&searchId=3XYjuS"    'Your Url
Sheets("Foglio7").Select                    '<<< The sheets that will be used
'
WPage.Get myUrl
WPage.Wait 500
Debug.Print ">Page loaded", Format(Timer - myTim, "0.0")
'
'Search the menu container:
'Set AColl = WPage.FindElementsByTag("div")
'For I = 1 To AColl.Count
'    If InStr(1, AColl(I).Attribute("style"), "top:", vbTextCompare) = 1 Then
'        Debug.Print ">Found menus DIV # " & I
'        Exit For
'    End If
'Next I
'MI = I
''...and click the second one:

Set tObj = WPage.FindElementByXPath("/html/body/div[1]/div[2]/div/div/main/div[2]/div/section[2]/div[2]/div[1]/div/div[1]/div[1]/button/div[2]")
tObj.Click

'Set tObj = AColl(MI).FindElementsByTag("svg")
'tObj(2).FindElementByXPath("./..").Click
WPage.Wait 500
'Search the RadioButtons Container:
Set BColl = WPage.FindElementsByClass("D_aIz")


'For I = 1 To BColl.Count
'    If InStr(1, BColl(I).Attribute("style"), "left:", vbTextCompare) = 1 Then
'        Debug.Print ">Found RadioButtons DIV # " & I
'        Exit For
'    End If
'Next I
'...and click the second one:
Set tObj = BColl(1).FindElementsByTag("input")
tObj(2).Click
WPage.Wait 500
Debug.Print ">ZxZx", tObj(2).Attribute("type"), tObj(2).Attribute("value")
'If InStr(1, AColl(MI).Text, "Recent", vbTextCompare) > 0 Then
'    Debug.Print ">Recent has been selected"
'    noBB = False
'Else
'    noBB = True
'End If
'Debug.Print ">noBB=" & noBB
'If noBB Then
'    AppActivate (Application.Caption)
'    MsgBox ("Selecting Recent failed; select it manually before closing the msgbox")
'    ''Stop            '<<< This is only for test
'End If
WPage.Wait 500
'Look for the Products:
Debug.Print ">Let's search the Products", Format(Timer - myTim, "0.0")
'Scroll to end of page..
WPage.ExecuteScript ("window.scrollTo(0, document.body.scrollHeight);")
WPage.Wait 1500
Set tObj = WPage.FindElementsByTag("Main")(1)
Set AColl = tObj.FindElementsByTag("div")
Debug.Print ">AColl.Count=" & AColl.Count
NextR = Cells(Rows.Count, 1).End(xlUp).Row + 1
For I = 1 To AColl.Count
    If InStr(1, AColl(I).Attribute("data-testid"), "listing-card", vbTextCompare) > 0 Then
        NextR = NextR + 1
        Debug.Print "Row " & NextR, "AColl Index=" & I, Format(Timer - myTim, "0.0")
        Cells(NextR, 1).Resize(1, 3).Clear
        Set BColl = AColl(I).FindElementsByTag("div")(1).FindElementsByTag("a")
        cCnt = BColl(2).FindElementsByTag("p").Count
        If cCnt > 2 Then
            Cells(NextR, 1) = BColl(2).FindElementsByTag("p")(cCnt - 3).Text & " "
            Cells(NextR, 2) = BColl(2).FindElementsByTag("p")(cCnt - 2).Text
            If BColl.Count > 1 Then
                ActiveSheet.Hyperlinks.Add anchor:=Cells(NextR, 1), _
                   Address:=BColl(2).Attribute("href")
            End If
        End If
    End If
Next I
Debug.Print ">Completed"
End Sub
Try, until it lasts...
 
Upvote 0
As I said, "These jobs are just a matter of time and patience"; and (I should have added) they have an expiration date. Good (for you) that the previous version lasted several months, till the site owner decides to change some commas here and there.

This is my revised code:
Code:
Sub Corousell5()
'per JasonLim - MrEx
'Vedi https://www.mrexcel.com/board/threads/vba-selenium-problems-no-such-element-error.1209120/#post-5918264
Dim WPage As Selenium.WebDriver, myUrl As String
Dim tObj As Object, myTim As Single, AColl As Object, BColl As Object
Dim noBB As Boolean, MI As Long
Dim NextR As Long, cCnt As Long
'
myTim = Timer
'Crea Driver:
'    Set WPage = CreateObject("Selenium.EdgeDriver")
    Set WPage = CreateObject("Selenium.CHRomedriver")
'
myUrl = "https://www.carousell.sg/search/toys?SearchId=GL7fvY&addrecent=true&canChangeKeyword=true&includeSuggestions=true&searchId=3XYjuS"    'Your Url
Sheets("Foglio7").Select                    '<<< The sheets that will be used
'
WPage.Get myUrl
WPage.Wait 500
Debug.Print ">Page loaded", Format(Timer - myTim, "0.0")
'
'Search the menu container:
'Set AColl = WPage.FindElementsByTag("div")
'For I = 1 To AColl.Count
'    If InStr(1, AColl(I).Attribute("style"), "top:", vbTextCompare) = 1 Then
'        Debug.Print ">Found menus DIV # " & I
'        Exit For
'    End If
'Next I
'MI = I
''...and click the second one:

Set tObj = WPage.FindElementByXPath("/html/body/div[1]/div[2]/div/div/main/div[2]/div/section[2]/div[2]/div[1]/div/div[1]/div[1]/button/div[2]")
tObj.Click

'Set tObj = AColl(MI).FindElementsByTag("svg")
'tObj(2).FindElementByXPath("./..").Click
WPage.Wait 500
'Search the RadioButtons Container:
Set BColl = WPage.FindElementsByClass("D_aIz")


'For I = 1 To BColl.Count
'    If InStr(1, BColl(I).Attribute("style"), "left:", vbTextCompare) = 1 Then
'        Debug.Print ">Found RadioButtons DIV # " & I
'        Exit For
'    End If
'Next I
'...and click the second one:
Set tObj = BColl(1).FindElementsByTag("input")
tObj(2).Click
WPage.Wait 500
Debug.Print ">ZxZx", tObj(2).Attribute("type"), tObj(2).Attribute("value")
'If InStr(1, AColl(MI).Text, "Recent", vbTextCompare) > 0 Then
'    Debug.Print ">Recent has been selected"
'    noBB = False
'Else
'    noBB = True
'End If
'Debug.Print ">noBB=" & noBB
'If noBB Then
'    AppActivate (Application.Caption)
'    MsgBox ("Selecting Recent failed; select it manually before closing the msgbox")
'    ''Stop            '<<< This is only for test
'End If
WPage.Wait 500
'Look for the Products:
Debug.Print ">Let's search the Products", Format(Timer - myTim, "0.0")
'Scroll to end of page..
WPage.ExecuteScript ("window.scrollTo(0, document.body.scrollHeight);")
WPage.Wait 1500
Set tObj = WPage.FindElementsByTag("Main")(1)
Set AColl = tObj.FindElementsByTag("div")
Debug.Print ">AColl.Count=" & AColl.Count
NextR = Cells(Rows.Count, 1).End(xlUp).Row + 1
For I = 1 To AColl.Count
    If InStr(1, AColl(I).Attribute("data-testid"), "listing-card", vbTextCompare) > 0 Then
        NextR = NextR + 1
        Debug.Print "Row " & NextR, "AColl Index=" & I, Format(Timer - myTim, "0.0")
        Cells(NextR, 1).Resize(1, 3).Clear
        Set BColl = AColl(I).FindElementsByTag("div")(1).FindElementsByTag("a")
        cCnt = BColl(2).FindElementsByTag("p").Count
        If cCnt > 2 Then
            Cells(NextR, 1) = BColl(2).FindElementsByTag("p")(cCnt - 3).Text & " "
            Cells(NextR, 2) = BColl(2).FindElementsByTag("p")(cCnt - 2).Text
            If BColl.Count > 1 Then
                ActiveSheet.Hyperlinks.Add anchor:=Cells(NextR, 1), _
                   Address:=BColl(2).Attribute("href")
            End If
        End If
    End If
Next I
Debug.Print ">Completed"
End Sub
Try, until it lasts...
Hi, thanks so much for replying to me. I tried the code, but somehow it fails a few times. I would like to know, who did you manage to the this

(Set tObj = WPage.FindElementByXPath("/html/body/div[1]/div[2]/div/div/main/div[2]/div/section[2]/div[2]/div[1]/div/div[1]/div[1]/button/div[2]")
tObj.Click)


Can you kindly share with me? I would like to learn more and unfortunately my knowledge is very weak. : (. Sorry for trouble you again. Your help will be very much appreciated. TQ
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top