Home > Net >  Selenium python chromedriver click to close cookie popup
Selenium python chromedriver click to close cookie popup

Time:09-30

I am using this website: https://www.marketscreener.com/stock-exchange/calendar/finance/

I have added various arguments and experimental options to disable popups but the pop-up you will see keeps coming up.

I am trying to close it by clicking the big green Accept & Close button but the error says that no such element exists

code:

url='https://www.marketscreener.com/stock-exchange/calendar/finance/'
logger.info('loading:',url)
options=webdriver.ChromeOptions()
chrome_prefs = {}
options.experimental_options["prefs"]=chrome_prefs
options.add_experimental_option("excludeSwitches",['disable-popup-blocking'])
chrome_prefs["profile.default_content_settings"] = {"popups":1}
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-infobars')
options.add_argument('--disable-extensions')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(executable_path=r'\\foo',options=options)
driver.get(url)


driver.find_element_by_xpath("//button[@title='Accept & Close']").click()

CodePudding user response:

I went to the URL on your question and no pop-up came up. But I have also had your problem. In my case putting,

import time
time.sleep(4) #change the seconds till it works for you

before driver.find_element_by_xpath("//button[@title='Accept & Close']").click() Worked. The second count will need a little tuning.

CodePudding user response:

Either apply Implicit wait:

driver.implicitly_wait(10)
driver.get(url)

Or Apply Explicit wait like below and try.

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

driver.get("https://www.marketscreener.com/stock-exchange/calendar/finance/")

wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@title='Accept & Close']"))).click()# put this line in try,except block if pop-up does not appear every time.
  • Related