Continuing in a loop until the error ceases or until 15 seconds have elapsed

My goal is to create a loop that attempts to click on an element until it is found. If this process exceeds a 15-second limit, a warning message will be displayed.

The current issue is that the loop does not respect the 15-second limit and gets stuck in an endless loop.

tlimit = 60
tfirst = Timer
On Error Resume Next
Do While Timer - tfirst < tlimit Or Err.Description <> ""

    drive.FindElementByXPath("//*[contains(@class, 'p-button-label') and text() = 'Erase']").Click

    If Err.Description = "" Then
        On Error GoTo 0
        Exit Do
    ElseIf Timer - tfirst > tlimit Then
        MsgBox " error" & Err.Description
    Else
        Err.Clear
        Application.Wait Now + TimeValue("00:00:01")
Loop

I chose to use Err.Description instead of Err.Number because Selenium's error for not finding the element returns as 0, resulting in no proper condition for the Do statement.

Answer №1

After testing your code, I found that it is operational. The only adjustment necessary is the timer duration, which is currently set to 60 seconds. You may want to consider reducing it to 15 seconds for optimal performance.

tlimit = 15
tfirst = Timer
On Error Resume Next
Do While Timer - tfirst < tlimit Or Err.Description <> ""
    drive.FindElementByXPath("//*[contains(@class, 'p-button-label') and text() = 'Erase']").Click
    If Err.Description = "" Then
        On Error GoTo 0
        Exit Do
    ElseIf Timer - tfirst > tlimit Then
        MsgBox " error" & Err.Description
    Else
        Err.Clear
        Application.Wait Now + TimeValue("00:00:01")
    End If
Loop

Answer №2

If the action to click on an element with a specific class and text fails due to a time limit, then a check is made to see if there is an error description generated during this process. If Err.Description is not empty, it means that the time limit has been exceeded and the process is allowed to continue.

Option Explicit

Private Sub errorHandlerTest()

Dim tlimit As Long
Dim tfirst As Double

Dim errDesc As String

tlimit = 15 '60
tfirst = Timer

Do While Timer - tfirst < tlimit

    Debug.Print
    Debug.Print " Do While Timer - tfirst < " & tlimit
    Debug.Print "  Timer - tfirst: " & Timer - tfirst
    
    errDesc = ""
    Debug.Print "   errDesc........: " & errDesc
    
    On Error Resume Next
    
    'Drive.FindElementByXPath("//*[contains(@class, 'p-button-label') and text() = 'Erase']").Click
    Err.Raise 0    ' for testing
    
    Debug.Print "   Err.Description: " & Err.Description
    
    If Err.Description = "" Then
        Exit Do
    End If
    
    errDesc = Err.Description
    
    On Error GoTo 0
    Debug.Print "   Err.Description: " & Err.Description
    Debug.Print "   errDesc........: " & errDesc
    
    Application.Wait Now + TimeValue("00:00:01")
    
Loop

If errDesc <> "" Then
    'MsgBox " error: " & errDesc
    Debug.Print
    Debug.Print " error: " & errDesc

Else
    Debug.Print
    Debug.Print " success"

End If

End Sub

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

WebDriver encountered an ElementNotInteractableException while attempting to locate an element

On my website, there is a menu that has the following code and I am attempting to automate it using Selenium WebDriver with Java. This is the HTML code for the menu: <a href="JavaScript:void(0); class="bars"> ::before ::after I have trie ...

What XPath expression can be used to locate this specific input element?

\<div class="css-1dbjc4n r-19m6qjp r-z2wwpe r-1loqt21 r-1x0uki6 r-1e081e0 r-1f1sjgu r-ah5dr5 r-1otgn73" data-focusable="true" tabindex="0" data-testid="login-submit-btn"\>\<div class="css ...

Following the release of Version 115, exciting new updates are available for Selenium VBA and

Before Version 115 of Chrome, I used to download the latest chromedriver to my local directory and everything would run smoothly with Selenium VBA! The exe file was downloaded from this link: https://chromedriver.chromium.org/downloads But now that my Chr ...

Identifying elements using xpath - Streamlined Xpath Techniques

I successfully found the element using the xpath copied directly from the code. Can someone assist me in creating a simpler xpath for the following code snippet, which is fully functional! WebElement oCheckbox = myDriver.findElement(By.xpath(".//*[@id=&ap ...

Jenkins launches Chrome with a restricted viewing size

My Selenium script (in Java) with Jenkins is using the Chrome browser. However, I am facing an issue where Jenkins opens the browser with dimensions 1040x784, despite my attempts to increase it to desktop browser size. The code I used to increase the dimen ...

Selenium facing issues with detecting a second window in Internet Explorer

When my application triggers a new window on button click, I encounter issues with the response of the getWindowHandles() method in selenium webdriver. It only returns one window id, especially when there is a delay in calling the getWindowHandles(). This ...

npm error - The module './selenium-webdriver/lib/input' cannot be located

After updating my Angular project from version 5 to 7, I encountered numerous vulnerabilities. To address this, I followed the suggested commands in "npm audit" which successfully fixed all the vulnerabilities. However, when attempting to run: ng serve ...

In the event that the element is not found, proceed with the task as planned. If an exception is raised due

I am attempting to create a function that will proceed with the job even if no element is found on this website. Here are some of the methods I have tried: try: except NoSuchElementException: pass Additionally, I have experimented with the use of if ...

I am encountering a NullPointerException while attempting to access a text box element that has been referenced from the POM file. Can anyone assist me in troubleshooting and resolving this issue?

Currently, I am delving into understanding the framework structure of Selenium with the Page Object model. Despite passing the webdriver instance successfully, I encounter a NullPointerException when trying to access a textbox element referenced from the P ...

Issue with clicking a button in Selenium using JavaScript for automation

I'm encountering an issue where Selenium is detecting an element as disabled, despite it being enabled. To work around this, I am attempting to click on the element using JavaScript with the following code snippet: IWebElement button = driver.FindEl ...

Using VBA and Selenium to access iframes within HTML with the #document tag

I am currently facing a challenge in accessing the HTML content within two iframes using Selenium Basic in VBA. Due to restrictions on our machines, we are unable to use IE and other tools like Python are not available to us. In the past, I was able to ac ...

The WebDriverException thrown states that the session [null] is currently unavailable and does not appear in the list of the last 1000 terminated sessions when using SeleniumGrid in Docker

While running Selenium grid with 2 connected nodes, I encountered an issue when attempting to execute a test script (.java). The error message received was: Exception in thread "Thread-23" org.openqa.selenium.WebDriverException: Session [null] not availab ...

Using VBA and Selenium to automate image uploading on a designated website

Looking to upload an image on a specific website, so I need the necessary code for this task Sub UploadImage() Dim bot As WebDriver Set bot = New WebDriver bot.Start "chrome" bot.Get "https://www.qrcode-mo ...

How can Behat and Mink be leveraged to choose the second or third option from a dropdown menu utilizing CSS selectors?

I am trying to use Behat Mink with Selenium2 to select the 2nd or 3rd item from a dropdown using CSS. However, I have not been successful using nth child selector. When I try selecting by item name, it works fine. But the items in the dropdown keep changi ...

What is the best way to handle a "NoSuchElementException" error and restart the loop?

I'm facing an issue with my code where sometimes a NoSuchElementException occurs and prevents the loop from finding the element upon refresh. How can I automatically restart the loop when this error happens and continue looping until the price conditi ...

The asynchronous callbacks or promises executing independently of protractor/webdriver's knowledge

Could a log like this actually exist? 07-<...>.js ... Stacktrace: [31m[31mError: Failed expectation[31m [31m at [object Object].<anonymous> (...06-....js)[31m[31m[22m[39m It seems that something is failing in file -06- while I am processin ...

Using several openVPN connections simultaneously for various internet browsers

My current goal is to utilize OpenVPN to create multiple connections so that each browser has its own unique IP address. I am attempting to achieve this programmatically using selenium, but I am open to using any other method if necessary. I have been foll ...

Sign in to a webpage using Python and Selenium

I am attempting to automatically log in to a website. Upon loading the main page, a login button appears that opens a form with fields for username and password. The login button is contained within a div element with an id of 'loginbox': <d ...

How to interact with an anchor tag in Selenium that contains a link to a JavaScript function

I've looked around but can't seem to find a solution for what I'm attempting to do. Currently, I'm working with Java and trying to access/click on an 'a' tag using Selenium. The problem is that I'm unsure of how to procee ...

Issue with special characters while exporting PHP data to Excel

I found some code on a website that is supposed to convert data from MySQL to Excel. Here is the link to the code I'm trying to use: When I try to save the Excel file, it works fine if there are no special characters. However, when it converts the ...