Loop until element equals to specific text in drop down












0















I have the following piece of code



Do
On Error Resume Next
.FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectByText ("txt")
On Error GoTo 0
Loop Until .FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectedOption.Text = "txt"


I have a lot of drop down lists that I deal with them with the same approach and although I used On Error Resume Next, I got errors sometimes and I have to wait a little and click Resume to resume the code execution
Can I make this as public procedure as I will use such lines a lot with other elements?
And how I can avoid the errors? and of course at the same time get my target for selecting the desired text in the drop down



Here's a snapshot of one of the errors
enter image description here



Based on @QHarr reply I tried to make a public procedure like that



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


End Sub



But when trying to use it in that way



WaitElement bot, .FindElementById("ContentPlaceHolder1_DropDownListnat"), ws.Range("B11").Value


I got 'Run-time error 13' (Type mismatch)



After applying the UDF named 'TextIsSet' I got this error
enter image description here



and the same problem.. if I click on Debug then Resume then wait a little, the code resumes its work



I have used such lines too but doesn't help



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


I got the same last error of not founding such an element










share|improve this question




















  • 1





    "how I can avoid the errors?" - how can we know unless you describe those errors? As for how to make this into a separate sub - there are only two variables in that code, so those would be obvious candidates as parameters in your sub.

    – Tim Williams
    Jan 2 at 17:53













  • The errors related to selenium that those elements that need waiting time .. even with the existence of the Do Loop Until .. I have to click Resume to make the code work and resume work

    – YasserKhalil
    Jan 2 at 17:57











  • I have attached an error .. I think it is a matter of waiting as when I click Debug then Resume .. the code works after a while

    – YasserKhalil
    Jan 2 at 18:28
















0















I have the following piece of code



Do
On Error Resume Next
.FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectByText ("txt")
On Error GoTo 0
Loop Until .FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectedOption.Text = "txt"


I have a lot of drop down lists that I deal with them with the same approach and although I used On Error Resume Next, I got errors sometimes and I have to wait a little and click Resume to resume the code execution
Can I make this as public procedure as I will use such lines a lot with other elements?
And how I can avoid the errors? and of course at the same time get my target for selecting the desired text in the drop down



Here's a snapshot of one of the errors
enter image description here



Based on @QHarr reply I tried to make a public procedure like that



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


End Sub



But when trying to use it in that way



WaitElement bot, .FindElementById("ContentPlaceHolder1_DropDownListnat"), ws.Range("B11").Value


I got 'Run-time error 13' (Type mismatch)



After applying the UDF named 'TextIsSet' I got this error
enter image description here



and the same problem.. if I click on Debug then Resume then wait a little, the code resumes its work



I have used such lines too but doesn't help



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


I got the same last error of not founding such an element










share|improve this question




















  • 1





    "how I can avoid the errors?" - how can we know unless you describe those errors? As for how to make this into a separate sub - there are only two variables in that code, so those would be obvious candidates as parameters in your sub.

    – Tim Williams
    Jan 2 at 17:53













  • The errors related to selenium that those elements that need waiting time .. even with the existence of the Do Loop Until .. I have to click Resume to make the code work and resume work

    – YasserKhalil
    Jan 2 at 17:57











  • I have attached an error .. I think it is a matter of waiting as when I click Debug then Resume .. the code works after a while

    – YasserKhalil
    Jan 2 at 18:28














0












0








0








I have the following piece of code



Do
On Error Resume Next
.FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectByText ("txt")
On Error GoTo 0
Loop Until .FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectedOption.Text = "txt"


I have a lot of drop down lists that I deal with them with the same approach and although I used On Error Resume Next, I got errors sometimes and I have to wait a little and click Resume to resume the code execution
Can I make this as public procedure as I will use such lines a lot with other elements?
And how I can avoid the errors? and of course at the same time get my target for selecting the desired text in the drop down



Here's a snapshot of one of the errors
enter image description here



Based on @QHarr reply I tried to make a public procedure like that



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


End Sub



But when trying to use it in that way



WaitElement bot, .FindElementById("ContentPlaceHolder1_DropDownListnat"), ws.Range("B11").Value


I got 'Run-time error 13' (Type mismatch)



After applying the UDF named 'TextIsSet' I got this error
enter image description here



and the same problem.. if I click on Debug then Resume then wait a little, the code resumes its work



I have used such lines too but doesn't help



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


I got the same last error of not founding such an element










share|improve this question
















I have the following piece of code



Do
On Error Resume Next
.FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectByText ("txt")
On Error GoTo 0
Loop Until .FindElementById("ContentPlaceHolder1_DropDownListl2").AsSelect.SelectedOption.Text = "txt"


I have a lot of drop down lists that I deal with them with the same approach and although I used On Error Resume Next, I got errors sometimes and I have to wait a little and click Resume to resume the code execution
Can I make this as public procedure as I will use such lines a lot with other elements?
And how I can avoid the errors? and of course at the same time get my target for selecting the desired text in the drop down



Here's a snapshot of one of the errors
enter image description here



Based on @QHarr reply I tried to make a public procedure like that



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


End Sub



But when trying to use it in that way



WaitElement bot, .FindElementById("ContentPlaceHolder1_DropDownListnat"), ws.Range("B11").Value


I got 'Run-time error 13' (Type mismatch)



After applying the UDF named 'TextIsSet' I got this error
enter image description here



and the same problem.. if I click on Debug then Resume then wait a little, the code resumes its work



I have used such lines too but doesn't help



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


I got the same last error of not founding such an element







excel vba selenium-webdriver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 20:18







YasserKhalil

















asked Jan 2 at 17:44









YasserKhalilYasserKhalil

1,3601416




1,3601416








  • 1





    "how I can avoid the errors?" - how can we know unless you describe those errors? As for how to make this into a separate sub - there are only two variables in that code, so those would be obvious candidates as parameters in your sub.

    – Tim Williams
    Jan 2 at 17:53













  • The errors related to selenium that those elements that need waiting time .. even with the existence of the Do Loop Until .. I have to click Resume to make the code work and resume work

    – YasserKhalil
    Jan 2 at 17:57











  • I have attached an error .. I think it is a matter of waiting as when I click Debug then Resume .. the code works after a while

    – YasserKhalil
    Jan 2 at 18:28














  • 1





    "how I can avoid the errors?" - how can we know unless you describe those errors? As for how to make this into a separate sub - there are only two variables in that code, so those would be obvious candidates as parameters in your sub.

    – Tim Williams
    Jan 2 at 17:53













  • The errors related to selenium that those elements that need waiting time .. even with the existence of the Do Loop Until .. I have to click Resume to make the code work and resume work

    – YasserKhalil
    Jan 2 at 17:57











  • I have attached an error .. I think it is a matter of waiting as when I click Debug then Resume .. the code works after a while

    – YasserKhalil
    Jan 2 at 18:28








1




1





"how I can avoid the errors?" - how can we know unless you describe those errors? As for how to make this into a separate sub - there are only two variables in that code, so those would be obvious candidates as parameters in your sub.

– Tim Williams
Jan 2 at 17:53







"how I can avoid the errors?" - how can we know unless you describe those errors? As for how to make this into a separate sub - there are only two variables in that code, so those would be obvious candidates as parameters in your sub.

– Tim Williams
Jan 2 at 17:53















The errors related to selenium that those elements that need waiting time .. even with the existence of the Do Loop Until .. I have to click Resume to make the code work and resume work

– YasserKhalil
Jan 2 at 17:57





The errors related to selenium that those elements that need waiting time .. even with the existence of the Do Loop Until .. I have to click Resume to make the code work and resume work

– YasserKhalil
Jan 2 at 17:57













I have attached an error .. I think it is a matter of waiting as when I click Debug then Resume .. the code works after a while

– YasserKhalil
Jan 2 at 18:28





I have attached an error .. I think it is a matter of waiting as when I click Debug then Resume .. the code works after a while

– YasserKhalil
Jan 2 at 18:28












1 Answer
1






active

oldest

votes


















1














This can happen when an action causes a change to the DOM. The lazy way is to add a timed loop to try for that element until that error goes away or time out reached. You could also try shifting the On Error to surround the loop instead of inside the loop and then add in a time out. This is a little brutal but without a webpage to test with.



As a function call (this feels ugly and you may find webElements don't like being passed around):



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




I don't have a stale element test case so I just used a drop down test case:



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





share|improve this answer


























  • Thanks a lot my tutor. I have a lot of elements with the same error .. so using such lines for each element will be painful ...!

    – YasserKhalil
    Jan 2 at 18:50











  • Thank you very much. I have attached an error after applying this change

    – YasserKhalil
    Jan 2 at 19:30











  • I will have a look. You have changed something as mine shouldn’t throw an error due to the ugly all encompassing on error resume next.

    – QHarr
    Jan 2 at 19:33











  • I have not changed anything. I have used the UDF as it is .. The drop down is a javascript element that filled with options .. is that make difference?

    – YasserKhalil
    Jan 2 at 19:43











  • you have a sub. See my udf above.

    – QHarr
    Jan 2 at 19:48












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010837%2floop-until-element-equals-to-specific-text-in-drop-down%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This can happen when an action causes a change to the DOM. The lazy way is to add a timed loop to try for that element until that error goes away or time out reached. You could also try shifting the On Error to surround the loop instead of inside the loop and then add in a time out. This is a little brutal but without a webpage to test with.



As a function call (this feels ugly and you may find webElements don't like being passed around):



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




I don't have a stale element test case so I just used a drop down test case:



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





share|improve this answer


























  • Thanks a lot my tutor. I have a lot of elements with the same error .. so using such lines for each element will be painful ...!

    – YasserKhalil
    Jan 2 at 18:50











  • Thank you very much. I have attached an error after applying this change

    – YasserKhalil
    Jan 2 at 19:30











  • I will have a look. You have changed something as mine shouldn’t throw an error due to the ugly all encompassing on error resume next.

    – QHarr
    Jan 2 at 19:33











  • I have not changed anything. I have used the UDF as it is .. The drop down is a javascript element that filled with options .. is that make difference?

    – YasserKhalil
    Jan 2 at 19:43











  • you have a sub. See my udf above.

    – QHarr
    Jan 2 at 19:48
















1














This can happen when an action causes a change to the DOM. The lazy way is to add a timed loop to try for that element until that error goes away or time out reached. You could also try shifting the On Error to surround the loop instead of inside the loop and then add in a time out. This is a little brutal but without a webpage to test with.



As a function call (this feels ugly and you may find webElements don't like being passed around):



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




I don't have a stale element test case so I just used a drop down test case:



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





share|improve this answer


























  • Thanks a lot my tutor. I have a lot of elements with the same error .. so using such lines for each element will be painful ...!

    – YasserKhalil
    Jan 2 at 18:50











  • Thank you very much. I have attached an error after applying this change

    – YasserKhalil
    Jan 2 at 19:30











  • I will have a look. You have changed something as mine shouldn’t throw an error due to the ugly all encompassing on error resume next.

    – QHarr
    Jan 2 at 19:33











  • I have not changed anything. I have used the UDF as it is .. The drop down is a javascript element that filled with options .. is that make difference?

    – YasserKhalil
    Jan 2 at 19:43











  • you have a sub. See my udf above.

    – QHarr
    Jan 2 at 19:48














1












1








1







This can happen when an action causes a change to the DOM. The lazy way is to add a timed loop to try for that element until that error goes away or time out reached. You could also try shifting the On Error to surround the loop instead of inside the loop and then add in a time out. This is a little brutal but without a webpage to test with.



As a function call (this feels ugly and you may find webElements don't like being passed around):



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




I don't have a stale element test case so I just used a drop down test case:



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





share|improve this answer















This can happen when an action causes a change to the DOM. The lazy way is to add a timed loop to try for that element until that error goes away or time out reached. You could also try shifting the On Error to surround the loop instead of inside the loop and then add in a time out. This is a little brutal but without a webpage to test with.



As a function call (this feels ugly and you may find webElements don't like being passed around):



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




I don't have a stale element test case so I just used a drop down test case:



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 19:27

























answered Jan 2 at 18:47









QHarrQHarr

36.7k82144




36.7k82144













  • Thanks a lot my tutor. I have a lot of elements with the same error .. so using such lines for each element will be painful ...!

    – YasserKhalil
    Jan 2 at 18:50











  • Thank you very much. I have attached an error after applying this change

    – YasserKhalil
    Jan 2 at 19:30











  • I will have a look. You have changed something as mine shouldn’t throw an error due to the ugly all encompassing on error resume next.

    – QHarr
    Jan 2 at 19:33











  • I have not changed anything. I have used the UDF as it is .. The drop down is a javascript element that filled with options .. is that make difference?

    – YasserKhalil
    Jan 2 at 19:43











  • you have a sub. See my udf above.

    – QHarr
    Jan 2 at 19:48



















  • Thanks a lot my tutor. I have a lot of elements with the same error .. so using such lines for each element will be painful ...!

    – YasserKhalil
    Jan 2 at 18:50











  • Thank you very much. I have attached an error after applying this change

    – YasserKhalil
    Jan 2 at 19:30











  • I will have a look. You have changed something as mine shouldn’t throw an error due to the ugly all encompassing on error resume next.

    – QHarr
    Jan 2 at 19:33











  • I have not changed anything. I have used the UDF as it is .. The drop down is a javascript element that filled with options .. is that make difference?

    – YasserKhalil
    Jan 2 at 19:43











  • you have a sub. See my udf above.

    – QHarr
    Jan 2 at 19:48

















Thanks a lot my tutor. I have a lot of elements with the same error .. so using such lines for each element will be painful ...!

– YasserKhalil
Jan 2 at 18:50





Thanks a lot my tutor. I have a lot of elements with the same error .. so using such lines for each element will be painful ...!

– YasserKhalil
Jan 2 at 18:50













Thank you very much. I have attached an error after applying this change

– YasserKhalil
Jan 2 at 19:30





Thank you very much. I have attached an error after applying this change

– YasserKhalil
Jan 2 at 19:30













I will have a look. You have changed something as mine shouldn’t throw an error due to the ugly all encompassing on error resume next.

– QHarr
Jan 2 at 19:33





I will have a look. You have changed something as mine shouldn’t throw an error due to the ugly all encompassing on error resume next.

– QHarr
Jan 2 at 19:33













I have not changed anything. I have used the UDF as it is .. The drop down is a javascript element that filled with options .. is that make difference?

– YasserKhalil
Jan 2 at 19:43





I have not changed anything. I have used the UDF as it is .. The drop down is a javascript element that filled with options .. is that make difference?

– YasserKhalil
Jan 2 at 19:43













you have a sub. See my udf above.

– QHarr
Jan 2 at 19:48





you have a sub. See my udf above.

– QHarr
Jan 2 at 19:48




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010837%2floop-until-element-equals-to-specific-text-in-drop-down%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory