Hi I am practicing and trying to close out of a pop up that occurs when you go to a site. There is an "x" in the top right corner that I want to click once the site opens. I get the site open but can’t figure out how to identify the "x" button ID to click and close it. I am new to HTML and Python. Using MAC/safari.
HTML code when I inspect the button to close the pop up
current code:
from selenium import webdriver
driver = webdriver.Safari()
driver.get('https://littlesleepies.com/collections/vip-early-access?utm_source=facebook&utm_medium=social&utm_campaign=vip_5_16_2024')
import time
def decline_offer():
decline = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//[@id='closeIconSvg']")))
decline.click()
Go easy on me I am a newbie lol.
2
Answers
Couple of things to try:
1st, it looks like you are trying to click on the icon element, rather than the button tag. Try //[@id=’closeIconContainer’] instead.
2nd, I know that you’re using Safari, but this is how I would find the xpath using Chrome.
Rather than using "//[@id=’closeIconSvg’]" as the xpath, find the element on the page, right click and select inspect. Click on the button element in the html, then right click again, find copy > copy Xpath then drop that in as your xpath.
It’s different for Svg
Try this:
//button[@id='closeIconContainer']/*[name()='svg'][@id='closeIconSvg']