-2

I want to click the "Akkoord" button but I am unable to do that. I already tried different methods but they are not working. Any help will be appreciated. enter image description here

one of the codes I tried.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.demorgen.be/nieuws')

time.sleep(20)
driver.find_elements_by_class_name('message-component message-button no-children pg-accept-button')[0].click() 
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Tauqeer Sajid
  • 101
  • 1
  • 1
  • 10

2 Answers2

0

use xpath or css

css :

[class="message-component message-button no-children pg-accept-button"]


driver.find_elements_by_css_selector('[class="message-component message-button no-children pg-accept-button"]')[0].click() 

xpath:

//*[@class="message-component message-button no-children pg-accept-button"]

driver.find_elements_by_xpath('//*[@class="message-component message-button no-children pg-accept-button"]')[0].click() 

find_elements_by_class_name expects single class name as argument thats why its not working as space in class indicates multiple classes.

THe find by class actually uses css under the hood. So if you want to find element having multiple class . You can replace space with '.' (THis works only in python)

driver.find_elements_by_class_name('message-component.message-button.no-children.pg-accept-button')[0].click() 

Also

The element is inside iframe

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



frame = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,
    "#sp_message_container_404503 iframe")))
driver.switch_to_frame(frame)
driver.find_element_by_css_selector(
    '[class="message-component message-button no-children pg-accept-button"]').click()
PDHide
  • 18,113
  • 2
  • 31
  • 46
0

The element Akkoord 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.get("https://www.demorgen.be/nieuws")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='sp_message_iframe']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Akkoord']"))).click()
      
    • Using XPATH:

      driver.get("https://www.demorgen.be/nieuws")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id, 'sp_message_iframe')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Akkoord']"))).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:

demorgen


Reference

You can find a couple of relevant discussions in:

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