Home > other >  Selenium - clicked windows does not open & no error
Selenium - clicked windows does not open & no error

Time:11-13

My code:

#Open Website
    profile_path = r'C:\Users\XXX\AppData\Local\Mozilla\Firefox\Profiles\ndefault-release'
    options = Options()
    options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
    options.set_preference('profile', profile_path)
    options.add_argument("--no-sandbox")        
    service = Service(r'C:\Users\XXX\geckodriver.exe')
    driver = Firefox(service=service, options=options)

    # declaration of variables 
    name = "x"
    suffix = "x"
    start_number = 1
    end_number = 1000

        for i in range(start_number, end_number):

            driver.get('https://www.bauhaus.info/gewinnspiel')
            time.sleep(3)
            #driver.find_element(by=By.XPATH, value=f"/div/div/div/div/div[2]/div/div[2]/div/div/div/button").click()        
            element = driver.execute_script("""return document.querySelector('#usercentrics-root').shadowRoot.querySelector("button[data-testid='uc-accept-all-button']")""")
            element.click()
            time.sleep(1)
            
            driver.switch_to.frame(0)
            time.sleep(1)
            driver.find_element(by=By.XPATH, value=f"/html/body/section/div/div/div/div[2]/div/div/div[1]/div[1]/form/div/div[3]/a").click()

            time.sleep(2)

If i add the xpath in the firefox console i get the right output:

Array [ a.btn.btn-primary ]

But the white windows does not open, only if i click manually on it:

enter image description here

CodePudding user response:

shadow_host = driver.find_element(By.ID, "usercentrics-root")
shadow_root = shadow_host.shadow_root

WebDriverWait(shadow_root, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "button[data-testid='uc-accept-all-button']"))).click()

iframe = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[title=' blackweekgewinnspiel']")))
driver.switch_to.frame(iframe)
canvas = driver.find_element(By.CSS_SELECTOR, "canvas")
driver.execute_script("arguments[0].scrollIntoView(true);",canvas)
driver.execute_script("arguments[0].click();", canvas);
driver.switch_to.default_content()

Two points to note:

  1. the Accept Cookies popup is in shadow DOM so selenium 4.1 or upwards is required to easily access that.
  2. the roulette wheel is in an iframe, so must switch there before clicking.
  • Related