0

I've a codeblock;

#Same code in Python
    try:
        io = myElm.get_attribute("rel")
        print(io)
    except IndexError:
        d_io = myElm.get_attribute("data-outcome")
        print(d_io)
    except:
        print("undefined error")

I'm working with selenium. When I search it, internet says there is no try-cath or try-expect method VBA. You can use On Error GoTo mehtod.

My purpose is, there is an element. Sometimes it doesnt have "rel" attribute, and it returns an ERROR. So if there is "rel" attribute. I'll take it, if there isnt not I'll take "data-outcome" attribute. Above code works perfect in python.

The code I can convert with my research on the internet is as follows.

The only problem, when I use GoTo a statement. I cannot be able to come back where I left. I tried like Resume Next smh else. It didnt work. Maybe I couldnt do it. BTW I'm using just for an application, just a newbie on VBA. Is there a way to turn back after use the GoTo method. Since I can't go back, the values created with the "rel" attribute change when I go to the goto statement. Anyways, Basically I've 3 statements such as;

On Error GoTo C1
If baglan.IsElementPresent(By.Css(myElm)) Then
Cells(sonsatir, 4) = baglan.FindElementByCss(myElm).Attribute("rel")
Else
Cells(sonsatir, 4) = baglan.FindElementByCss(myElm).Attribute("data-outcome")
End If


On Error GoTo C2
If baglan.IsElementPresent(By.Css(myElm)) Then
Cells(sonsatir, 5) = baglan.FindElementByCss(myElm).Attribute("rel")
Else
Cells(sonsatir, 5) = baglan.FindElementByCss(myElm).Attribute("data-outcome")
End If


On Error GoTo C3
If baglan.IsElementPresent(By.Css(myElm)) Then
Cells(sonsatir, 6) = baglan.FindElementByCss(myElm).Attribute("rel")
Else
Cells(sonsatir, 6) = baglan.FindElementByCss(myElm).Attribute("data-outcome")
End If

'Some codes here'

 C1:
 Cells(sonsatir, 4) = baglan.FindElementByCss(myElm).Attribute("data-outcome")

 C2:
 Cells(sonsatir, 5) = baglan.FindElementByCss(myElm).Attribute("data-outcome")

 C3:
 Cells(sonsatir, 6) = baglan.FindElementByCss(myElm).Attribute("data-outcome")
SIM
  • 21,997
  • 5
  • 37
  • 109
Fatih Tüz
  • 133
  • 1
  • 12
  • https://stackoverflow.com/questions/6028288/properly-handling-errors-in-vba-excel – braX Jun 03 '20 at 18:42
  • I saw this, but when i try resume next to go back where i left. It doesnt work – Fatih Tüz Jun 03 '20 at 18:44
  • You can solve one problem by putting Exit Sub just before C1:, in other words between your code and the error handlers. – Brian M Stafford Jun 03 '20 at 18:52
  • You arent really handling or clearing the errors within your error trapping. read the link again. Your example code also does not include any `Resume` code so your question is not clear. – braX Jun 03 '20 at 19:34
  • You don't need to do that `On Error Resume Next` thing to get the content you are after. There is a better way of doing this in vba selenium binding. Could you share the site url? – SIM Jun 04 '20 at 08:11

1 Answers1

0

Use only Resume instead of Resume Next.

Resume comes back to where the error occurred.

Resume Next jumps to the next row after the error occurred.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93