Selenium : switch to modal window handle gives error

Question:

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("[email protected]")
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

enter image description here

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.

Asked By: Himanshuman

||

Answers:

You don’t need window handles to handle a modal window. You may directly identify the element and interact with it.
Here is the sample. In this, you provide input in the credentials field and click on Sign-in. After this, the modal appears. The code interacts with the modal (As a sample, and an ethical means, I just closed it, i.e., I clicked on the X icon. And the modal closes. After this, the username field is cleared of the text that was input.
Usually, the robot verification is done to check if it is human that is interacting or is it a bot, and the websites have their own algorithms to understand if its clicked by a bot, i.e., there are chances that you’d be blocked if the bot action is detected.

driver.get('https://account.proton.me/login?language=en')
usernameField = driver.find_element(By.XPATH,'//*[@id="username"]')
usernameField.send_keys("[email protected]")
passwordField = driver.find_element(By.XPATH,'//*[@id="password"]')
passwordField.send_keys("yehbhikuchbhi")
loginbutton = driver.find_element(By.XPATH,'//button[@type="submit"]')
loginbutton.click()
time.sleep(5)
driver.find_element(By.XPATH, "//*[@class='modal-two-dialog-container']//div/div//following-sibling::div").click()
usernameField.clear()
passwordField.clear()
time.sleep(5)
driver.quit()
Answered By: Anand Gautam

Problem is that you have 2 nested iframes 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.

Answered By: Kfir Doron