0

How to get and click "dismiss-button" inside Google vignette? I've already tried the standard solution from other posts but I get "NoSuchElementException" The last digit in "aswift_" iframe is variable so I'm using a partial name match like this:

WebDriverWait(self.driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[contains(@name, 'aswift_')]")))

# doesn't work:
self.driver.find_element(By.ID, 'dismiss-button').click()

or:

# doesn't work:
self.driver.find_element(By.XPATH, "//*[contains(@id, 'dismiss-button')]".click() 

To get that vignette you have to:

  1. Enter https://www.automationexercise.com/
  2. Click "Products"

enter image description here

Thanks for your support!

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24

1 Answers1

0

The element X within the google vignette popup is a SVG element.


Solution

To click on the svg element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • Using cssSelector:

    driver.get(url='https://www.automationexercise.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Products"))).click()
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='aswift_4']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#dismiss-button[aria-label='Close ad'] svg path"))).click()
    
  • Using xpath:

    driver.get(url='https://www.automationexercise.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Products"))).click()
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='aswift_4']")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='dismiss-button' and @aria-label='Close ad']//*[name()='svg']//*[name()='path']"))).click()
    
  • Browser snapshot:

automationexercise

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352