Search code examples
pythonseleniumxpathcss-selectorswebdriverwait

How to click the below shown icon using Python Selenium?


I have attached a screenshot of a Stackoverflow page.

How to click it with Python Selenium?

reference the image to identify what I am pointing to


Solution

  • To click on the notification icon you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "svg.svg-icon.iconInbox"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[local-name()='svg' and @class='svg-icon iconInbox']"))).click()
      
    • Note: You have to add the following imports :

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