Problem working with selenium on protonmail.com

Viewed 131

I want to create email on protonmail.com with selenium But I can not go beyond the sign up page i can not find username field and send text to it. Why??what is the problem ?? And what is the solution?

my code

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.webdriver.common.keys import Keys
import time
from selenium import webdriver
from random import randint
#setuser=set()
#def randomuser():
#    name='ahmad'
#    num=randint(10000,999999)
#    return f'{name}{num}{name[:randint(0,5)]}'

d=webdriver.Chrome()
d.get('https://protonmail.com/signup')
WebDriverWait(d, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="signup-plans"]/div[5]/div[1]/div[1]/div/div[3]/i[1]'))).click()
WebDriverWait(d, 4).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="freePlan"]'))).click()
WebDriverWait(d, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="password"]'))).send_keys('12345678')
WebDriverWait(d, 4).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="repeat-password"]'))).send_keys('12345678')

try:
    time.sleep(3)
    d.find_element_by_css_selector('#username').send_keys('username')
    d.find_element_by_css_selector('#username').submit()
        
except:
    WebDriverWait(d, 10).until(EC.frame_to_be_available_and_switch_to_it(d.find_element_by_xpath('//iframe')))
    user=d.find_element_by_xpath('//body[@class="color-norm bg-norm sign-layout-container"]//*[@id="username"]')
    user.send_keys('username')
    user.send_keys(Keys.ENTER)
    time.sleep(3)
    user.send_keys(Keys.ENTER)

1 Answers

The username field is inside an iframe.
To access it you have to switch to that iframe first.
Also, username is not unique id value.
Also, you need not to define wait object each time.
The following should fix your code:

wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="signup-plans"]/div[5]/div[1]/div[1]/div/div[3]/i[1]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="freePlan"]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="password"]'))).send_keys('12345678')
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="repeat-password"]'))).send_keys('12345678')
d.switch_to.frame(driver.find_element_by_tag_name("iframe"))
d.find_element_by_xpath("//body[contains(@class,'color-norm')]//input[@id='username']").send_keys('username')
d.find_element_by_xpath("//body[contains(@class,'color-norm')]//input[@id='username']").submit()

UPD
Please keep in mind, that after switching to iframe, in order to access elements out of that iframe you will have to switch back to the nornal content with

driver.switch_to.default_content()
Related