Debug School

rakesh kumar
rakesh kumar

Posted on

Explain NoSuchElementException in selenium python

NoSuchElementException in Selenium occurs when the WebDriver cannot locate a web element on the web page. This exception is quite common when automating web tests. Here are different scenarios when a NoSuchElementException can occur, along with coding examples:

Element Not Present

Using Incorrect Locator

Element Inside an Iframe

Timing Issue

Element Not Loaded

Hidden Element

Element Loaded via AJAX

Element Not Visible or Interactable

Element Not Clickable

In this scenario, you attempt to find an element that doesn't exist on the page.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_id("nonexistent-element")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In Selenium, you can handle the NoSuchElementException exception when an element is not present on a web page by using a try and except block. Here's an example of how to do this in Python with a sample HTML page:

HTML Code (example.html):

<!DOCTYPE html>
<html>
<head>
    <title>Element Not Present Example</title>
</head>
<body>
    <!-- This element is not present initially -->
    <p id="not-present-element">This element is not present initially.</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Attempt to find the element (this will raise NoSuchElementException if not found)
    not_present_element = driver.find_element_by_id("not-present-element")

    # If the element is found, you can perform actions on it
    print("Element text:", not_present_element.text)

except NoSuchElementException as e:
    # Handle the exception when the element is not present
    print("Element not found:", e)

finally:
    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Replace file:///path/to/your/example.html with the actual file path to your HTML file, and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver).

In this code, we use a try and except block to attempt to find an element with the ID "not-present-element." If the element is not found, it will raise a NoSuchElementException exception, and we handle that exception by printing a message.

This approach allows you to gracefully handle the scenario where an element may or may not be present on a web page without causing the script to crash.

Using Incorrect Locator:

This scenario occurs when you use an incorrect or invalid locator to find an element.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_class_name("invalid-class")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In Selenium, when you use an incorrect locator to find an element, it can result in a NoSuchElementException if the element is not found on the web page. Here's an example of how to handle this exception in Python:

HTML Code (example.html):

<!DOCTYPE html>
<html>
<head>
    <title>Incorrect Locator Example</title>
</head>
<body>
    <button id="click-button">Click Me</button>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Attempt to find the element using an incorrect locator
    incorrect_element = driver.find_element_by_id("incorrect-id")

    # If the element is found, you can perform actions on it
    print("Element text:", incorrect_element.text)

except NoSuchElementException as e:
    # Handle the exception when the element is not found
    print("Element not found:", e)

finally:
    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Replace file:///path/to/your/example.html with the actual file path to your HTML file, and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver).

In this code, we attempt to find an element with the ID "incorrect-id," which does not exist in the HTML. This will result in a NoSuchElementException, and we handle that exception by printing a message.

Handling this exception allows you to gracefully handle cases where you might be using incorrect locators, ensuring that your script does not crash and providing you with error information for debugging.

Element Inside an Iframe:

When the element is inside an iframe, you need to switch to the iframe first. Not doing so will result in a NoSuchElementException.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    driver.switch_to.frame("iframe-id")
    element = driver.find_element_by_id("element-inside-iframe")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

When dealing with elements inside an iframe using Selenium in Python, you may encounter a NoSuchElementException if the iframe is not switched to correctly before attempting to locate elements within it. Here's an example of how to handle this exception when working with elements inside an iframe:

HTML Code (example.html):

<html>
<head>
    <title>IFrame Example</title>
</head>
<body>
    <h1>IFrame Example</h1>
    <iframe id="my-iframe" src="iframe_content.html"></iframe>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

HTML Code for the iframe content (iframe_content.html):

<!DOCTYPE html>
<html>
<head>
    <title>IFrame Content</title>
</head>
<body>
    <p id="iframe-paragraph">This is a paragraph inside the iframe.</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page with the iframe
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Switch to the iframe by its ID or name
    driver.switch_to.frame("my-iframe")

    # Attempt to find an element inside the iframe
    iframe_paragraph = driver.find_element_by_id("iframe-paragraph")

    # If the element is found, you can perform actions on it
    print("Element text inside iframe:", iframe_paragraph.text)

except NoSuchElementException as e:
    # Handle the exception when the element is not found inside the iframe
    print("Element not found inside iframe:", e)

finally:
    # Switch back to the default content (outside the iframe)
    driver.switch_to.default_content()

    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In this example, we have an HTML page containing an iframe element, and the iframe contains a paragraph element. We switch to the iframe using driver.switch_to.frame("my-iframe") to locate the element inside it. If the element is not found inside the iframe, it will raise a NoSuchElementException, and we handle that exception by printing an error message.

Remember to replace file:///path/to/your/example.html with the actual file path to your HTML file and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver). Also, make sure that you provide the correct iframe identifier (ID or name) when switching to the iframe.

Timing Issue:

Sometimes, elements load asynchronously, and the WebDriver tries to find them before they are available. Using explicit waits can help in such cases.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "delayed-element")))
    element = driver.find_element_by_id("delayed-element")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()

Enter fullscreen mode Exit fullscreen mode

When dealing with timing issues in Selenium, you may encounter a NoSuchElementException if you attempt to locate an element before it becomes available or visible on the page. To handle such timing issues, you can use explicit waits. Here's an example of how to handle this exception when dealing with timing issues:

HTML Code (example.html):

<!DOCTYPE html>
<html>
<head>
    <title>Timing Issue Example</title>
</head>
<body>
    <button id="load-button">Load Content</button>
    <div id="content" style="display:none;">
        <p>This content was loaded dynamically.</p>
    </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Click the "Load Content" button to load the content dynamically
    load_button = driver.find_element_by_id("load-button")
    load_button.click()

    # Wait for the content to become visible (timeout set to 10 seconds)
    content = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "content"))
    )

    # Find an element inside the loaded content
    loaded_paragraph = content.find_element(By.TAG_NAME, "p")

    # If the element is found, you can perform actions on it
    print("Loaded element text:", loaded_paragraph.text)

except NoSuchElementException as e:
    # Handle the exception when the element is not found
    print("Element not found:", e)

finally:
    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In this example, we have a button that, when clicked, dynamically loads content into a hidden div. We use an explicit wait to wait for the content to become visible using EC.visibility_of_element_located before attempting to locate an element within it.

If the element is not found inside the loaded content, it will raise a NoSuchElementException, and we handle that exception by printing an error message.

Remember to replace file:///path/to/your/example.html with the actual file path to your HTML file, and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver).

Element Not Loaded:

This occurs when you try to interact with an element before the page has fully loaded, or AJAX requests are still in progress.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com/slow-loading-page")
    element = driver.find_element_by_id("element-before-load")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

When dealing with elements that may not be loaded due to a timing issue or other reasons in Selenium, you may encounter a NoSuchElementException if you attempt to locate the element before it is actually present on the page. To handle this exception, you can use explicit waits. Here's an example of how to handle the NoSuchElementException when dealing with elements that are not loaded:

HTML Code (example.html):

<!DOCTYPE html>
<html>
<head>
    <title>Element Not Loaded Example</title>
</head>
<body>
    <button id="load-button">Load Element</button>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Click the "Load Element" button to trigger the element load
    load_button = driver.find_element_by_id("load-button")
    load_button.click()

    # Wait for the element to become present (timeout set to 10 seconds)
    loaded_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "loaded-element"))
    )

    # If the element is found, you can perform actions on it
    print("Loaded element text:", loaded_element.text)

except NoSuchElementException as e:
    # Handle the exception when the element is not loaded
    print("Element not loaded:", e)

finally:
    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In this example, we have a button that, when clicked, triggers the loading of an element with the ID "loaded-element." We use an explicit wait to wait for the element to become present on the page using EC.presence_of_element_located before attempting to locate and interact with it.

If the element is not loaded within the specified timeout or is not present on the page, it will raise a NoSuchElementException, and we handle that exception by printing an error message.

Remember to replace file:///path/to/your/example.html with the actual file path to your HTML file and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver).

Hidden Element:

Hidden elements are not considered when locating elements. If an element is hidden, you won't be able to find it using standard locators.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_id("hidden-element")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

When dealing with hidden elements in Selenium, you may encounter a NoSuchElementException if you attempt to locate an element that is not visible on the page. To handle this exception, you can use explicit waits to ensure the element is both present and visible. Here's an example of how to handle the NoSuchElementException when dealing with hidden elements:

HTML Code (example.html):

<!DOCTYPE html>
<html>
<head>
    <title>Hidden Element Example</title>
</head>
<body>
    <button id="show-button">Show Element</button>
    <div id="hidden-element" style="display:none;">This is a hidden element.</div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Click the "Show Element" button to make the element visible
    show_button = driver.find_element_by_id("show-button")
    show_button.click()

    # Wait for the element to become visible (timeout set to 10 seconds)
    hidden_element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "hidden-element"))
    )

    # If the element is found and visible, you can perform actions on it
    print("Hidden element text:", hidden_element.text)

except NoSuchElementException as e:
    # Handle the exception when the element is not found or not visible
    print("Element not found or not visible:", e)

finally:
    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In this example, we have a button that, when clicked, makes a hidden element with the ID "hidden-element" visible on the page. We use an explicit wait to wait for the element to become visible using EC.visibility_of_element_located before attempting to locate and interact with it.

If the element is not found or not visible within the specified timeout, it will raise a NoSuchElementException, and we handle that exception by printing an error message.

Remember to replace file:///path/to/your/example.html with the actual file path to your HTML file and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver).

Element Loaded via AJAX:

Elements loaded dynamically via AJAX may not be available immediately. Using explicit waits can help in such cases.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
try:
    driver.get("https://example.com/ajax-page")
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "ajax-loaded-element")))
    element = driver.find_element_by_id("ajax-loaded-element")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

When dealing with elements that are loaded via AJAX requests in Selenium, you may encounter a NoSuchElementException if you attempt to locate the element before it is loaded on the page. To handle this exception, you can use explicit waits to ensure the element is both present and visible after the AJAX request completes. Here's an example of how to handle the NoSuchElementException when dealing with elements loaded via AJAX:

HTML Code (example.html):

<!DOCTYPE html>
<html>
<head>
    <title>AJAX Element Loading Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <button id="load-button">Load Element via AJAX</button>
    <div id="ajax-container"></div>
    <script>
        // Function to simulate an AJAX request
        function loadElementViaAjax() {
            $.get("https://jsonplaceholder.typicode.com/posts/1", function (data) {
                $("#ajax-container").html("<p id='loaded-element'>" + data.title + "</p>");
            });
        }

        // Click event handler to trigger the AJAX request
        $("#load-button").click(function () {
            loadElementViaAjax();
        });
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Click the "Load Element via AJAX" button to trigger the AJAX request
    load_button = driver.find_element_by_id("load-button")
    load_button.click()

    # Wait for the element to become visible (timeout set to 10 seconds)
    loaded_element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "loaded-element"))
    )

    # If the element is found and visible, you can perform actions on it
    print("Loaded element text:", loaded_element.text)

except NoSuchElementException as e:
    # Handle the exception when the element is not found or not visible
    print("Element not found or not visible:", e)

finally:
    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In this example, we have a button that, when clicked, triggers an AJAX request to load an element with the ID "loaded-element" into a container div. We use an explicit wait to wait for the element to become visible using EC.visibility_of_element_located before attempting to locate and interact with it.

If the element is not found or not visible within the specified timeout, it will raise a NoSuchElementException, and we handle that exception by printing an error message.

Remember to replace file:///path/to/your/example.html with the actual file path to your HTML file and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver).
Element Not Visible or Interactable:

Elements that are not visible or not interactable cannot be located or interacted with.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_id("not-visible-element")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

When dealing with elements that are not visible or interactable in Selenium, you may encounter a NoSuchElementException if you attempt to interact with such an element before it becomes visible or interactable on the page. To handle this exception, you can use explicit waits to ensure the element is both present and visible/interactable. Here's an example of how to handle the NoSuchElementException when dealing with elements that are not visible or interactable:

HTML Code (example.html):

<!DOCTYPE html>
<html>
<head>
    <title>Not Visible or Interactable Element Example</title>
    <style>
        #hidden-element {
            display: none;
        }
    </style>
</head>
<body>
    <button id="show-button">Show Element</button>
    <input type="text" id="input-element" style="display: none;">
    <div id="hidden-element">This element is not visible initially.</div>
    <script>
        // Function to make the input element visible and interactable
        function showInputElement() {
            document.getElementById("input-element").style.display = "block";
        }
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Click the "Show Element" button to make the input element visible and interactable
    show_button = driver.find_element_by_id("show-button")
    show_button.click()

    # Wait for the input element to become visible (timeout set to 10 seconds)
    input_element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "input-element"))
    )

    # Attempt to interact with the input element
    input_element.send_keys("Selenium Interaction")

    # If the element is found and interactable, you can perform actions on it
    print("Input element value:", input_element.get_attribute("value"))

except NoSuchElementException as e:
    # Handle the exception when the element is not found or not interactable
    print("Element not found or not interactable:", e)

finally:
    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In this example, we have an input element that is initially hidden, and a button that, when clicked, makes the input element visible and interactable. We use an explicit wait to wait for the input element to become visible using EC.visibility_of_element_located before attempting to locate and interact with it.

If the element is not found or not visible/interactable within the specified timeout, it will raise a NoSuchElementException, and we handle that exception by printing an error message.

Remember to replace file:///path/to/your/example.html with the actual file path to your HTML file and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver).

Incorrect Locator Strategy:

Using an incorrect locator strategy can lead to a NoSuchElementException.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element(By.XPATH, "//div[@class='nonexistent']")
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Element Not Clickable:

When attempting to interact with a non-clickable element, you'll encounter this exception.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_id("non-clickable-element")
    element.click()  # Raises NoSuchElementException
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

When dealing with elements that are not clickable in Selenium, you may encounter a NoSuchElementException if you attempt to click such an element before it becomes clickable on the page. To handle this exception, you can use explicit waits to ensure the element is both present and clickable. Here's an example of how to handle the NoSuchElementException when dealing with elements that are not clickable:

HTML Code (example.html):

<!DOCTYPE html>
<html>
<head>
    <title>Not Clickable Element Example</title>
</head>
<body>
    <button id="disable-button" disabled>Disabled Button</button>
    <script>
        // Function to enable the button after a delay
        function enableButton() {
            setTimeout(function () {
                document.getElementById("disable-button").removeAttribute("disabled");
            }, 2000);
        }
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Python Code (example.py):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

# Initialize the WebDriver (you need to have a compatible webdriver installed, e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open the web page
driver.get("file:///path/to/your/example.html")  # Replace with the actual path

try:
    # Call the JavaScript function to enable the button after a delay
    driver.execute_script("enableButton();")

    # Wait for the button to become clickable (timeout set to 10 seconds)
    clickable_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "disable-button"))
    )

    # Attempt to click the button
    clickable_button.click()

    # If the element is found and clickable, you can perform actions on it
    print("Button text:", clickable_button.text)

except NoSuchElementException as e:
    # Handle the exception when the element is not found or not clickable
    print("Element not found or not clickable:", e)

finally:
    # Close the browser window
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

In this example, we have a button element that is initially disabled, and a JavaScript function is called to enable the button after a delay of 2 seconds. We use an explicit wait to wait for the button to become clickable using EC.element_to_be_clickable before attempting to locate and click it.

If the element is not found or not clickable within the specified timeout, it will raise a NoSuchElementException, and we handle that exception by printing an error message.

Remember to replace file:///path/to/your/example.html with the actual file path to your HTML file and ensure that you have the appropriate WebDriver installed (in this example, we're using ChromeDriver).

Top comments (0)