I want to close the popup for the site https://shopee.com.my/ in selenium Please check the below image.
I tried below, but getting errors like
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".shopee-popup__close-btn"}
Code:
driver.find_element_by_class_name("shopee-popup__close-btn").click()
CodePudding user response:
It's in shadow root.
Code:
driver_path = r'C:\\Users\\***\\***\\chromedriver.exe'
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://shopee.com.my/")
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='English']"))).click()
try:
time.sleep(3)
close_btn = driver.execute_script('return document.querySelector("#main shopee-banner-popup-stateful").shadowRoot.querySelector("div.home-popup__close-area div.shopee-popup__close-btn")')
close_btn.click()
print('Clicked successfully')
except:
print('Could not clicked')
pass
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC