How to click and close the banner after accessing the website https://www.aliexpress.com/ using Python and Selenium

309 views Asked by At

I'm trying to access a well known online shopping service, to filter and sort results, however the service has a "popup" offering coupons when you first open the site. I'm trying to dismiss the message. The problem is the popup, isn't a real popup, it's a series of div and img tags which are dynamically created shortly after visiting the homepage.

I've tried searching by class, Xpath, CSS but no luck. Can anyone tell me what I'm doing wrong

driver = webdriver.Chrome(options=opts, executable_path='chromedriver')
driver.delete_all_cookies()
driver.get('https://www.aliexpress.com/')

timeout = 30

try:
    WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH,'//*[@id="6216442440"]/div/div/img')))
except TimeoutException:
    driver.quit()
1

There are 1 answers

5
undetected Selenium On BEST ANSWER

The element to dismiss the message is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.aliexpress.com/')
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://campaign.aliexpress.com/wow/gcp/']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img.rax-image[src^='https://img.alicdn.com/tfs']"))).click()
    
  • Using XPATH:

    driver.get('https://www.aliexpress.com/')
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://campaign.aliexpress.com/wow/gcp/')]")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[@class='rax-image ' and starts-with(@src, 'https://img.alicdn.com/tfs')]"))).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
    
  • Browser Snapshot:

aliexpress_deals


Reference

You can find a couple of relevant discussions in: