pythonseleniumselenium-webdriverbotssoundcloud

How do I Switch to Iframe and enter text in field with Selenium


Im attempting to build bot in Selenium and python that logs into SoundCloud and from their performs a few other tasks. I have gotten selenium to click the login button, but from there im having trouble getting selenium to click the input field and enter text inside the subsequent Iframe for email, may someone offer me some advice please? :)

from tkinter import *
import random
import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import requests
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options



driver = webdriver.Chrome(executable_path='/Users/quanahbennett/PycharmProjects/SeleniumTest/chromedriver')
url= "https://soundcloud.com/"
driver.get(url)
#time.sleep(30)

wait = WebDriverWait(driver, 30)
#link = driver.find_elements_by_link_text("Sign in")
#link[0].click()
#driver.execute_script("arguments[0].click();", link[0])


# Click on Sign-in

#SUCCESFUL LOGIN BUTTON PUSH
please = driver.find_element_by_css_selector('button.frontHero__loginButton')
please.click()

try:
    driver.switch_to.frame(0)

    try:
        attempt = driver.find_element_by_id('sign_in_up_email')
        attempt.send_keys('Email')
    except:
        print("This is where the problem is occuring")
except:
    print("didnt succesfully transfer to iframe")



breakpoint()





#driver.quit()

Solution

  • The iframe has a class-name - webAuthContainer__iframe. Can make use of the same to switch to the iframe.

    please = driver.find_element_by_css_selector('button.frontHero__loginButton')
    please.click()
    
    # Switch to iframe using class-name
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME,"webAuthContainer__iframe")))
    
    attempt = wait.until(EC.element_to_be_clickable((By.ID,"sign_in_up_email")))
    attempt.send_keys("example@email.com")
    
    # Switch to default content to interact with elements outside the iframe
    driver.switch_to.default_content()