0

I am trying to automate some download of data and have an issue with accepting the cookies message.

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

from selenium.webdriver.common.by import By
from random import randint
import time

current_link = "https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F"
driver = webdriver.Chrome(PATH)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(current_link)
#driver.switch_to.frame("cookieConsentIframe")
#driver.switch_to.frame(driver.find_element_by_name('cookieConsentIframe'))

try:
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='cookieConsentIframe']")))
    print(1)
    driver.find_element(By.XPATH,'//button[@id="cookies-accept-all"]').click()
    #driver.find_element(By.XPATH,'//button[text()="Accept"]').click()
except:
    pass
 
time.sleep(randint(5,8))   
driver.quit()

The code runs through (prints also the 1) but never clicks the button. Any suggestions? Tried so many things already.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Rolf12
  • 701
  • 1
  • 6
  • 20

2 Answers2

2

You need to also wait out the button:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="cookies-accept-all"]'))).click()

EDIT Here is a full example (selenium/chromedriver setup is for linux, but you need to observe only the imports, and part after defining the browser):

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC

import time as t

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)



url = 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'
browser.get(url)

WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='cookieConsentIframe']")))
    
## sortout cookie button
try:
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="cookies-accept-all"]'))).click()
    print("accepted cookies")
except Exception as e:
    print('no cookie button')
Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
0

The element Accept is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      driver.execute("get", {'url': 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'})
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.cookieConsent#cookieConsentIframe")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#cookies-accept-all span.btn-accept"))).click()
      
    • Using XPATH:

      driver.execute("get", {'url': 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'})
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='cookieConsent' and @id='cookieConsentIframe']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='cookies-accept-all']//span[@class='btn-accept']"))).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
    
  • Browser Snapshot:

smapone

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352