0

Hi I'm new to Selenium and trying to do some automation on a webpage. When it first loads up a pop up window is displayed. When I inspect the element the close button appears as follows

<span title="Close" class="js-close-class offer-close"></span>

I am trying to close this pop up with the following code in my script:

driver.get("https://www.oddschecker.com/")
driver.implicitly_wait(5)
element = driver.find_element_by_class_name("js-close-class offer-close")
element.click()
time.sleep(10)
driver.quit()

This code is unable to find the element and will not close the pop up. Can anyone help me?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

1 Answers1

-1

To click on the desired element you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "span.js-close-class.offer-close[title='Close']").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//span[@class='js-close-class offer-close' and @title='Close']").click()
    

Ideally, to click on the element 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, "span.js-close-class.offer-close[title='Close']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='js-close-class offer-close' and @title='Close']"))).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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217