带有 Chrome 的 VBA Selenium - 只想在 VBA 中添加错误处理程序

约瑟夫·约翰
  1. 想在Error Handler中添加两个东西,如果脚本进入stuck任何一行,就像网页不符合要求一样,它会显示Alert pop
  2. 对于这个网页要求就像,姓名字符长度必须> 3 或手机号码数字必须= 10
  3. 然后我想刷新‪bot.Refresh页面然后想要 - 在警报弹出窗口中单击确定(或接受)bot.SwitchToAlert.Accept然后Sub End- 所有这些内容都在错误处理中。
  4. 成功运行错误处理后,脚本必须从头开始运行(来自 #Patient Name)
  5. 我想在 Excel 中添加一列,其中哪一行出现错误,给我未完成,没有错误给我完成
Sub Automate()
    Dim bot As WebDriver
    Set bot = New WebDriver
    bot.Start "chrome"
    bot.Get "/"
    
'#Patient Name
    bot.FindElementByName("patient_name").SendKeys (Sheet1.Cells(row, 2).Value)

'#Age-in
    If Sheet1.Cells(row, 4) = "Years" Then
        bot.FindElementById("age_year").Click
        bot.FindElementByName("age").SendKeys (Sheet1.Cells(row, 5).Value)
    Else
    If Sheet1.Cells(row, 4) = "Months" Then
        bot.FindElementById("age_month").Click
        bot.FindElementByName("age").SendKeys (Sheet1.Cells(row, 5).Value)
    Else
    If Sheet1.Cells(row, 4) = "Days" Then
        bot.FindElementById("age_day").Click
        bot.FindElementByName("age").SendKeys (Sheet1.Cells(row, 5).Value)
    End If
    End If
    End If

'#Mobile Number
    bot.FindElementById("contact_number").SendKeys (Sheet1.Cells(row, 3).Value)

'#Gender
    If Sheet1.Cells(row, 6) = "Male" Then
        Set ListDD = bot.FindElementById("gender")
        ListDD.AsSelect.SelectByText (Sheet1.Cells(row, 6).Value)
    Else
    If Sheet1.Cells(row, 6) = "Female" Then
        Set ListDD = bot.FindElementById("gender")
        ListDD.AsSelect.SelectByText (Sheet1.Cells(row, 6).Value)
    Else
    If Sheet1.Cells(row, 6) = "Transgender" Then
        Set ListDD = bot.FindElementById("gender")
        ListDD.AsSelect.SelectByText (Sheet1.Cells(row, 6).Value)
    End If
    End If
    End If
End Sub

在此处输入图片说明

罗伊

要处理 VBA 中的错误,您可以使用On Error Goto <Label>On Error Resume Next

对于On Error GoTo <Label>如果运行时异常发生时执行控制转到<Label>块,并从开始执行那里。

因为On Error Resume Next它只是忽略异常并从下一行开始执行。乍一看,它可能看起来很愚蠢,因为它可能会导致一系列错误并导致不可预测的行为。然而,经过精心开发,它确实有自己的使用费用份额。

在这种特殊情况下On Error Goto <Label>会更合适,它看起来像这样。

Sub Automate()
START: 'Labled code block indicating start of our execution
    On Error GoTo ERROR_HANDLE:
    'Your code
    
    
    
    Sheet1.Cells(Row, 10).Value = "DONE"            'Change the column index according to your column of choice
    Exit Sub    'Or Exit Function if it is inside a function
ERROR_HANDLE: 'Labled code block defining error handling behavior
    bot.Refresh
    bot.SwitchToAlert.Accept
    Sheet1.Cells(Row, 10).Value = "NOT DONE"        'Change the column index according to your column of choice
    'If you want to start again from start immediately from here uncomment next line. However this can potentially become an infinite loop.
    'GoTo START
    
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

带有GOTO的VBA错误处理程序并乘以错误

如何使用 selenium VBA 打开带有扩展名的 chrome

VBA:错误处理程序中的错误

无法使用Selenium VBA在Chrome中循环Excel数据

使用 Selenium VBA 切换回 chrome 中的父窗口

VBA中的错误处理

为什么只有一个 VBA 错误处理程序处理错误?

带“ With”语句的VBA错误处理程序

在 VBA 中插入带有变量的公式

VBA中带有for循环的求和条件

确定所有行是否有故障或某些行正确-Excel VBA中的错误处理

尝试使用Selenium + Excel VBA从Chrome浏览器中的网站上抓取代码

如何检查 vba(Excel) selenium 中的“chrome”webdriver 是否仍处于活动状态

DOM无法使用Selenium(IE,Chrome和VBA)刷新

如何使用 VBA/selenium/chrome 查找 href 属性?

如何使用Selenium和VBA设置Chrome下载目录

在Selenium VBA中使用Google Chrome(安装步骤)

VBA错误处理程序:On Error Resume在处理程序中不起作用

Excel VBA中带有长变量的错误6

想在 VBA 中创建多个 if 语句

无法使用带有VBA的Selenium从网页的下拉列表中选择复选框

如何在 VBA 中使用多个错误处理程序

带有收集对象参考的VBA Excel错误424

带有 .quit 的 vba excel 运行时错误 438

我有一个Ansible文件。我只想在Flask API中运行ansible文件

我只想在行中显示反转的单词,但它也显示了所有行的反转

VBA中的错误处理-在错误继续时继续

Excel VBA在带有表的新表中设置样式

从Excel获取带有VBA的PowerPoint中形状的尺寸