davvpo
davvpo

Reputation: 49

Selenium Python: 'Close' button on pop-up window not interactable

Can't seem to click on a 'close' button to close a pup up window that I have opened before to scrape the data that it displays. The button has the following html script:

<div class="closebutton" onclick="return hs.close(this)" title="Schließen"></div>

I was trying the following:

driver.find_element_by_xpath("//div[@class='closebutton']").click()

But I get the error:

Message: element not interactable

Anyone have a clue how to make the element interactable?

Upvotes: 0

Views: 1084

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

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

  • Using css_selector:

    driver.find_element_by_css_selector("button.closebutton[title='Schließen']").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//div[@class='closebutton' and @title='Schließen']").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, "button.closebutton[title='Schließen']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='closebutton' and @title='Schließen']"))).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
    

Upvotes: 1

Related Questions