如何切换到 Iframe 并使用 Selenium 在字段中输入文本

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

我正在尝试在 Selenium 中构建机器人,并且 python 登录到 SoundCloud 并从中执行一些其他任务。我已经获得了点击登录按钮的硒,但是从那里我无法获得硒来点击输入字段并在随后的 Iframe 中输入文本以发送电子邮件,有人可以给我一些建议吗? :)

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()

iframe 有一个 class-name - webAuthContainer__iframe。可以利用同样的方法切换到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()