Selin Elibol
Selin Elibol

Reputation: 111

Python Selenium closing a pop-up window

I'd like to close this window opened up on Amazon web site by using Selenium with Python. I've tried find_element_by_xpath, but it doesn't work. Here's the snippet of the code;

close_to_list = browser.get("/html/body/div[4]/div/div/div[2]/div[2]/div[2]/div[1]/div[2]/div/div/table/tbody/tr[2]")

I get the xpath of that 'X' button but I guess I need to close it as switch_to_alert, but I'm new to this era so I couldn't write it properly.

Here's the image view; enter image description here

Upvotes: 1

Views: 5241

Answers (1)

Andersson
Andersson

Reputation: 52685

You don't need to use switch_to_alert() as well as get() to close modal window. Just try to close it with below code:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label="Close"]'))).click()

This allows you to wait for button appearance and click the button

Upvotes: 3

Related Questions