简体   繁体   English

将 python try-except(或 try-catch)代码块转换为 VBA 以用于基本的 excel-selenium 应用程序

[英]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.我正在使用 selenium。 When I search it, internet says there is no try-cath or try-expect method VBA.当我搜索它时,互联网说没有 try-cath 或 try-expect 方法 VBA。 You can use On Error GoTo mehtod.您可以使用 On Error GoTo 方法。

My purpose is, there is an element.我的目的是,有一个元素。 Sometimes it doesnt have "rel" attribute, and it returns an ERROR.有时它没有“rel”属性,它返回一个错误。 So if there is "rel" attribute.所以如果有“rel”属性。 I'll take it, if there isnt not I'll take "data-outcome" attribute.我会接受它,如果没有,我会接受“数据结果”属性。 Above code works perfect in python.上面的代码在 python 中完美运行。

The code I can convert with my research on the internet is as follows.我在互联网上的研究可以转换的代码如下。

The only problem, when I use GoTo a statement.唯一的问题,当我使用 GoTo 语句时。 I cannot be able to come back where I left.我无法回到我离开的地方。 I tried like Resume Next smh else.我试过像Resume Next smh 一样。 It didnt work.它没有用。 Maybe I couldnt do it.也许我做不到。 BTW I'm using just for an application, just a newbie on VBA.顺便说一句,我仅用于应用程序,只是 VBA 上的新手。 Is there a way to turn back after use the GoTo method.使用 GoTo 方法后有没有办法返回。 Since I can't go back, the values created with the "rel" attribute change when I go to the goto statement.由于我无法返回 go,因此当我将 go 更改为 goto 语句时,使用“rel”属性创建的值会发生变化。 Anyways, Basically I've 3 statements such as;无论如何,基本上我有 3 个陈述,例如;

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而不是Resume Next

Resume comes back to where the error occurred. Resume回到错误发生的地方。

Resume Next jumps to the next row after the error occurred. Resume Next在错误发生后跳转到下一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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