-2

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
PSR
  • 249
  • 1
  • 10

3 Answers3

1

That's an SVG element, and I do see that the class svg-icon iconInbox is unique in nature, so the below XPath should work for you.

//*[name()='svg' and @class='svg-icon iconInbox']

and using explicit wait, you can perform click like this:

wait = WebDriverWait(driver, 30)
try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @class='svg-icon iconInbox']"))).click()
    print('Clicked on the button')
except:
    print('Could not click ')
    pass

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
1

The selector for the Stack Overflow inbox is "svg.iconInbox".

You can verify that directly from the console with:

document.querySelector("svg.iconInbox")

Add that to a click method in a Selenium framework, and you can click it. Eg, here's how to click it using SeleniumBase:

self.click("svg.iconInbox")
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352