当前位置: 首页 > 知识库问答 >
问题:

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

相诚
2023-03-14

我正试图在Selenium和python中构建一个bot,它可以登录到SoundCloud并从它们的服务器执行一些其他任务。我已经让selenium点击了登录按钮,但是从那里我很难让selenium点击输入字段并在随后的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()

共有1个答案

侯英达
2023-03-14

iframe有一个类名-webAuthContainer\uuu 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()
 类似资料:
  • 问题内容: 我正在尝试使用Selenium,Python和BS4在iframe中访问iframe 在返回的文本中,似乎还有两个iframe。我将如何访问那些?我尝试上面的代码没有成功。 问题答案: 将返回到文档顶部。发生的事情是您切换到第一个,然后又切换回文档的顶部,然后尝试查找第二个。硒找不到第二个,因为它在第一个之内。 如果删除第二个,则应该没问题:

  • 问题内容: 我正在尝试使用Selenium,Python和BS4在iframe中访问iframe 在返回的文本中,似乎还有两个iframe。我将如何访问这些?我尝试上面的代码没有成功。 问题答案: 将返回到文档顶部。发生的事情是,您切换到第一个,切换回文档的顶部,然后尝试查找第二个。硒找不到第二个,因为它在第一个之内。 如果删除第二个,则应该没问题:

  • 我在selenium查找输入文本字段时遇到了问题--'billing-address__line-1我使用的代码是- 错误信息 线程“main”org.openqa.selenium.nosuchelementException:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“.//fieldset[.//input[@id='billing-addres

  • 问题内容: 当我在文本字段中输入文本时,它将被删除。 这是代码: 问题答案: 同意Subir Kumar Sao和Faiz。

  • 当我在文本字段中输入文本时,它会被删除。 代码如下:

  • 在我试图制作的一个程序中,有三个文本字段和几个按钮(每个按钮代表一个数字)。我正在尝试获取它,这样你就可以通过点击按钮(而不是键盘)来打字了。到目前为止,我得到了这个: 在搜索互联网后,我发现了以下代码: 唯一的问题是,我不知道如何使用我找到的代码(我找到的代码是第二个块)。 我从哪里得到密码的。