Can't click on a specific element using selenium/python (tried iframe and all the methods that I know or found here)

Question:

I am trying to reach a website using Selenium and Python.

Once the website is loaded, there is like an iframe/pop-up above the website and I need to click on "Alles akzeptieren" but I can’t target the element no matter what method I try.

I left all the methods that I used commented out, but neither one worked.

import re
import time
from time import sleep
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
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.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import os,sys, glob
import requests
import win32api
import openpyxl
import xlsxwriter
import io
import csv
from csv import writer
import getpass
from time import sleep
import pandas as pd
import pyperclip as pc
import shutil
import requests
import win32api
from datetime import datetime, date


timestampstart = time.strftime("%Y%m%d-%H-%M-%S")
print("start: " + timestampstart)


user= os.environ.get('USERNAME')
file_name = str(os.path.basename(sys.argv[0]))


now = datetime.now()
path = os.path.abspath(".")
runPath = path
usrName = getpass.getuser()

print("Automation started!")



os.environ['WDM_LOG_LEVEL'] = '0'
options = Options()
options.headless = False######

options.add_argument("--log-level=3")
options.add_argument("--disable-logging")
prefs = {'download.default_directory' : runPath}
options.add_experimental_option('prefs', prefs)
options.add_experimental_option('excludeSwitches', ['enable-logging'])
selenium = webdriver.Chrome(ChromeDriverManager().install(), options=options)
selenium.maximize_window()



  
wait = WebDriverWait(selenium, 60, poll_frequency=1)


selenium.execute_script("window.open('about:blank','firsttab');")
selenium.switch_to.window("firsttab")      
selenium.get('https://www.conrad.de/')
WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
selenium.implicitly_wait(1)
time.sleep(1)

################################################################################################################################
####################### switch to iframe id uc-cross-domain-bridge


#selenium.switch_to.frame(selenium.find_elements_by_tag_name("iframe")[0])
#selenium.switch_to.frame(selenium.find_element_by_id("uc-cross-domain-bridge"))
#selenium.find_element_by_xpath("//button[contains(., 'Alle')]").click()
#yourElement = selenium.find_elements_by_xpath(".//button[@class='sc-gsDKAQ gvhqmO']")    
#selenium.execute_script("arguments[0].click();", yourElement)

selenium.switch_to.frame(selenium.find_element_by_css_selector("body>iframe"))
but = selenium.find_elements_by_tag_name('div')
for b in but:
    if 'Alle' in b.text:
        try:
            b.click()
        except:
            selenium.execute_script("arguments[0].click();", b)
            time.sleep(1)
            break
selenium.switch_to_default_content()




try:
    WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
    but = selenium.find_elements_by_tag_name('div')
    for b in but:
        if 'Alle' in b.text:
            try:
                b.click()
            except:
                selenium.execute_script("arguments[0].click();", b)
                time.sleep(1)
                break
except:
    pass
#################################################################################################################################

try:
    butt = selenium.find_elements_by_class_name('sc-gsDKAQ gvhqmO')
    butt.click()
    selenium.execute_script("arguments[0].click();", butt)
    for bb in butt:
        if 'Alles' in bb.text:
            try:
                bb.click()

            except:
                selenium.execute_script("arguments[0].click();", bb)
                time.sleep(1)
                break
except:
    pass

Don’t know why I got -1…but, posting my latest tries:

import re
import time
from time import sleep
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
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.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import os,sys, glob
import requests
import win32api
import openpyxl
import xlsxwriter
import io
import csv
from csv import writer
import getpass
from time import sleep
import pandas as pd
import pyperclip as pc
import shutil
import requests
import win32api
from datetime import datetime, date


timestampstart = time.strftime("%Y%m%d-%H-%M-%S")
print("start: " + timestampstart)


user= os.environ.get('USERNAME')
file_name = str(os.path.basename(sys.argv[0]))


now = datetime.now()
path = os.path.abspath(".")
runPath = path
usrName = getpass.getuser()

print("Automation started!")



os.environ['WDM_LOG_LEVEL'] = '0'
options = Options()
options.headless = False######

options.add_argument("--log-level=3")
options.add_argument("--disable-logging")
prefs = {'download.default_directory' : runPath}
options.add_experimental_option('prefs', prefs)
options.add_experimental_option('excludeSwitches', ['enable-logging'])
selenium = webdriver.Chrome(ChromeDriverManager().install(), options=options)
selenium.maximize_window()



  
wait = WebDriverWait(selenium, 60, poll_frequency=1)


selenium.execute_script("window.open('about:blank','firsttab');")
selenium.switch_to.window("firsttab")      
selenium.get('https://www.conrad.de/')
WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
selenium.implicitly_wait(1)
time.sleep(1)

################################################################################################################################
####################### switch to iframe id uc-cross-domain-bridge


#selenium.switch_to.frame(selenium.find_elements_by_tag_name("iframe")[0])
#selenium.switch_to.frame(selenium.find_element_by_id("uc-cross-domain-bridge"))
#selenium.find_element_by_xpath("//button[contains(., 'Alle')]").click()
#yourElement = selenium.find_elements_by_xpath(".//button[@class='sc-gsDKAQ gvhqmO']")    
#selenium.execute_script("arguments[0].click();", yourElement)
#try:
#    selenium.find_element_by_xpath("//*[contains(text(), 'Alles akzeptieren')]").click()
#except:
#    selenium.find_element_by_xpath("//div[.='Alles akzeptieren']/parent::button").click()
try:
    selenium.switch_to.frame(selenium.find_element_by_css_selector("body>iframe"))
    but = selenium.find_elements_by_tag_name('div')
    for b in but:
        if 'Alles akzeptieren' in b.text:
            try:
                b.click()
            except:
                selenium.execute_script("arguments[0].click();", b)
                time.sleep(1)
                break
    selenium.switch_to.default_content()
except:
    pass



try:
    WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
    but = selenium.find_elements_by_tag_name('div')
    for b in but:
        if 'Alle' in b.text:
            try:
                b.click()
            except:
                selenium.execute_script("arguments[0].click();", b)
                time.sleep(1)
                break
except:
    pass
#################################################################################################################################

try:
    butt = selenium.find_elements_by_class_name('sc-gsDKAQ gvhqmO')
    butt.click()
    selenium.execute_script("arguments[0].click();", butt)
    for bb in butt:
        if 'Alles' in bb.text:
            try:
                bb.click()

            except:
                selenium.execute_script("arguments[0].click();", bb)
                time.sleep(1)
                break
except:
    pass
#####################################
selenium.switch_to.frame(selenium.find_element_by_css_selector("body>iframe"))
but = selenium.find_elements_by_tag_name('div')
for b in but:
    if 'Alles akzeptieren' in b.text:
        try:
            b.click()
        except:
            selenium.execute_script("arguments[0].click();", b)
            time.sleep(1)
            break
selenium.switch_to.default_content()




try:
    WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
    but = selenium.find_elements_by_tag_name('div')
    for b in but:
        if 'Alle' in b.text:
            try:
                b.click()
            except:
                selenium.execute_script("arguments[0].click();", b)
                time.sleep(1)
                break
except:
    pass
#################################################################################################################################

try:
    butt = selenium.find_elements_by_class_name('sc-gsDKAQ gvhqmO')
    butt.click()
    selenium.execute_script("arguments[0].click();", butt)
    for bb in butt:
        if 'Alles' in bb.text:
            try:
                bb.click()

            except:
                selenium.execute_script("arguments[0].click();", bb)
                time.sleep(1)
                break
except:
    pass
a = selenium.find_element_by_class_name('ccpAEM')
aa = a.find_element_by_id('usercentrics-root')
b = aa.find_element_by_class_name('root responsivegrid')
aaa = b.find_element_by_class_name('sc-bdvvtL ljqtvs')#
aaaa = aaa.find_element_by_id('focus-lock-id')
aaaaa = aaaa.find_element_by_id('focus-lock-id')
aaaaaa = aaaaa.find_element_by_class_name('sc-pVTFL')# gsAnRF
aaaaaaa = aaaaaa.find_element_by_class_name('sc-jrQzAO')# CTXfe
aaaaaaaa = aaaaaaa.find_element_by_class_name('sc-bYoBSM')# egarKh
aaaaaaaaa = aaaaaaaa.find_element_by_class_name('sc-kLwhqv')# fslbnd
aaaaaaaaaa = aaaaaaaaa.find_element_by_class_name('sc-dlVxhl')# bEDIID
aaaaaaaaaaa = aaaaaaaaaa.find_element_by_class_name('sc-kfPuZi')# jQTRpX
aaaaaaaaaaaa = aaaaaaaaaaa.find_element_by_class_name('sc-gsDKAQ')# gvhqmO
print(aaaaaaaaaaaa.text)

Still can’t target the element
It uses shadow root

Asked By: Pythonless

||

Answers:

Try using https://pypi.org/project/pyshadow/ in order to select your element.

Answered By: Catalin