简体   繁体   中英

converting python try-except (or try-catch) code block to VBA for basic excel-selenium application

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")

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM