简体   繁体   中英

Selenium : switch to modal window handle gives error

I have this webpage https://account.proton.me/login?language=en where I am trying to switch to modal after logging in to the page. Please note that the modal appears even if you give wrong id and password, so to reproduce you can use the same code below

driver.get('https://account.proton.me/login?language=en')
usernameField = driver.find_element(By.XPATH,'//*[@id="username"]')
usernameField.send_keys("kuchbhirandom@some.thing")
passwordField = driver.find_element(By.XPATH,'//*[@id="password"]')
passwordField.send_keys("yehbhikuchbhi")
loginbutton = driver.find_element(By.XPATH,'//button[@type="submit"]')
loginbutton.click()

The above code gives us the modal

在此处输入图像描述

I ttried checking the window handles and switching to them one by one which gives me

driver.window_handles
['CDwindow-34B695696D2295F87F84F06321D10117', 'CDwindow-212C47AEC0CCD8240A4F9675D5B5BEF2', 'CDwindow-5A39DFE9B9C75CFA7316BF2098765D05', 'CDwindow-796B9EF6777039A036CCF7C3D452807A', 'CDwindow-1DF355426CF56902DC339955FF55C9AE', 'CDwindow-1B700B499C716086FA269E89B0ED3835']
for handle in driver.window_handles:
    driver.switch_to.window(handle)
    try:
        checkbox = driver.find_element(By.XPATH,'//div[@id="checkbox"]')
        print('found')
    except:
        pass

but I get the same error "No such element"

Talking about solving the captch: I have an external API that does it for me, but I need to click that check box here, but stuck in switching to the modal part

Note that: to reproduce issue you can use same code above, no need to create account.

Problem is that you have 2 nested iframe s in the site, You'll have to perform driver.switch_to.frame() for each of them:

# After pressing "sign-in"
captcha_iframe = driver.find_element(By.CSS_SELECTOR, '[title="Captcha"]')
driver.switch_to.frame(captcha_iframe)
inner_iframe = driver.find_element(By.CSS_SELECTOR, 'iframe')
driver.switch_to.frame(inner_iframe)
# Perform captcha check
driver.find_element(By.CSS_SELECTOR, '#checkbox').click()

Notice that after each switch_to , I directly use the new driver's context, and I do not perform a search under the element I found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM