Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 循环直到元素等于下拉列表中的特定文本_Excel_Vba_Selenium Webdriver - Fatal编程技术网

Excel 循环直到元素等于下拉列表中的特定文本

Excel 循环直到元素等于下拉列表中的特定文本,excel,vba,selenium-webdriver,Excel,Vba,Selenium Webdriver,我有下面的代码 Do On Error Resume Next .FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectByText ("txt") On Error GoTo 0 Loop Until .FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectedOption.Text = "txt" 我

我有下面的代码

Do
    On Error Resume Next
    .FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectByText ("txt")
    On Error GoTo 0
Loop Until .FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectedOption.Text = "txt"
我有很多下拉列表,我用同样的方法处理它们,尽管我在下一步的错误恢复中使用了它们,但我有时会出错,我必须等待一段时间,然后单击“恢复”来恢复代码执行 我是否可以将此作为公共程序,因为我将在其他元素中大量使用此类行? 我怎样才能避免这些错误呢?当然,同时在下拉列表中获得选择所需文本的目标

下面是其中一个错误的快照

基于@QHarr的回复,我试图制定一个这样的公开程序

Sub WaitElement(driver As Selenium.WebDriver, sElement As SelectElement, txt As String)
Dim t           As Date
Const MAX_SEC   As Long = 30

With driver
    On Error Resume Next
        t = Timer
        Do
            DoEvents
            sElement.AsSelect.SelectByText txt
            If Timer - t > MAX_SEC Then Exit Do
        Loop Until sElement.AsSelect.SelectedOption.Text = txt
    On Error GoTo 0
End With
端接头

但是当你试图用这种方式使用它的时候

WaitElement bot, .FindElementById("ContentPlaceHolder1_DropDownListnat"), ws.Range("B11").Value
我发现“运行时错误13”类型不匹配

应用名为“TextIsSet”的自定义项后,我出现了此错误

同样的问题。。如果单击Debug然后Resume,然后稍等,代码将继续工作

我也用过这样的台词,但没用

        Do
    Loop While .FindElementsById("ContentPlaceHolder1_Dschool").Count = 0

最后一个错误是没有创建这样的元素,当一个操作导致DOM发生更改时,可能会发生这种情况。懒惰的方法是添加一个计时循环来尝试该元素,直到错误消失或达到超时。您还可以尝试将On错误转移到循环周围,而不是循环内部,然后添加超时。这是一个有点残酷,但没有一个网页来测试

作为函数调用,这感觉很难看,您可能会发现webElements不喜欢被传递:

Option Explicit
Public Sub test()
    Const MAX_WAIT_SEC As Long = 30
    'other code
    If TextIsSet(dropdown, expectedText, MAX_WAIT_SEC) Then

    End If

End Sub

Public Function TextIsSet(ByRef dropdown As Object, ByVal expectedText As String, ByVal MAX_WAIT_SEC As Long) As Boolean
    Dim t As Date
    On Error Resume Next
    t = Timer
    Do
        DoEvents
        dropdown.AsSelect.SelectByText expectedText
        If Timer - t > MAX_WAIT_SEC Then Exit Do
    Loop Until dropdown.AsSelect.SelectedOption.Text = expectedText
    If dropdown.AsSelect.SelectedOption.Text = expectedText Then
        TextIsSet = True
    Else
        TextIsSet = False
    End If
    On Error GoTo 0
End Function
我没有过时的元素测试用例,所以我只使用了一个下拉测试用例:

Option Explicit
Public Sub test()
    Const MAX_WAIT_SEC As Long = 30
    Dim d As WebDriver, expectedText As String, dropdown As Object
    'expectedText = "AL - Alabama"  ''Pass Case
     expectedText = "Bananaman" 'Fail Case
    Set d = New ChromeDriver

    With d       
        .get "https://tools.usps.com/zip-code-lookup.htm?byaddress"           
        Set dropdown = .FindElementById("tState")  
        'other code
        If TextIsSet(dropdown, expectedText, MAX_WAIT_SEC) Then
            Debug.Print "Tada"
        Else
            Debug.Print "Sigh"
        End If
        .Quit
    End With
End Sub

Public Function TextIsSet(ByRef dropdown As Object, ByVal expectedText As String, ByVal MAX_WAIT_SEC As Long) As Boolean
    Dim t As Date
    On Error Resume Next
    t = Timer
    Do
        DoEvents
        dropdown.AsSelect.SelectByText expectedText
        If Timer - t > MAX_WAIT_SEC Then Exit Do
    Loop Until dropdown.AsSelect.SelectedOption.Text = expectedText
    If dropdown.AsSelect.SelectedOption.Text = expectedText Then
        TextIsSet = True
    Else
        TextIsSet = False
    End If
    On Error GoTo 0
End Function

我如何避免这些错误除非你描述这些错误,否则我们怎么知道?至于如何将其变成一个单独的子元素,代码中只有两个变量,因此这些变量显然可以作为子元素中的参数。与selenium相关的错误是那些需要等待时间的元素。。即使存在Do循环,直到。。我必须单击Resume使代码正常工作,Resume正常工作我附加了一个错误。。我认为这是一个等待的问题,就像我单击“调试”然后单击“恢复”一样。。代码在一段时间后就可以运行了,这让我的导师非常感激。我有很多元素都有相同的错误。。所以对每个元素使用这样的行将是痛苦的。。。!非常感谢你。应用此更改后,我附加了一个错误。我将查看。你已经改变了一些东西,因为我不应该抛出一个错误,因为丑陋的全面错误简历下。我没有改变任何东西。我已经按原样使用了UDF。。下拉列表是一个充满选项的javascript元素。。这有区别吗?你有一个潜艇。见上面我的udf。