【问题标题】:How to send text into a input field within a frame using Python Selenium如何使用 Python Selenium 将文本发送到框架内的输入字段
【发布时间】:2018-09-04 14:32:00
【问题描述】:

我是 Selenium 的初学者(使用 Python),我试图做一个简单的应用程序来登录我的报纸网站,但我在输入不同框架中的登录字段时遇到了问题。我环顾四周,发现了几个答案,这些答案显示了如何选择不同的框架。但是,我已经这样做了,但是当我尝试将文本发送到登录字段时,什么也没有发生。

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

url = 'http://www.valor.com.br'
path_to_chromedriver = 'chromedriver' 
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
browser.get(url)
browser.find_element_by_id("login-valor").click()
wait = WebDriverWait(browser,10)
WebEl = wait.until(EC.presence_of_element_located((By.ID,'cadunLoginModal')))

browser.switch_to_frame(browser.find_element_by_id("cadunLoginModal").find_element_by_tag_name("iframe"))
browser.find_element_by_id("login").send_keys("teste")

我尝试将“teste”发送到登录字段,但没有出现任何内容。

如果我通过点击尝试替换最后一行...

browser.find_element_by_id("login").click()

...我最终收到以下消息:

  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <iframe src="https://login.globo.com/login/6668?url=&amp;tam=WIDGET" style="height: 100%; width: 100%;" cd_frame_id_="4bed04b195abe0173e2400a792245d31"></iframe> is not clickable at point (599, 298). Other element would receive the click: <iframe src="https://login.globo.com/login/6668?url=&amp;tam=WIDGET" style="height: 100%; width: 100%;"></iframe>
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db),platform=Mac OS X 10.13.6 x86_64)

该字段看起来好像不可见,但确实如此。

您能帮我解释一下这个问题吗?非常感谢。

【问题讨论】:

    标签: python selenium iframe css-selectors webdriver


    【解决方案1】:

    根据 url http://www.valor.com.br 发送一个字符序列到你需要的登录字段:

    • WebDriverWait 元素作为 Login 的文本诱导为可点击。
    • 诱导 WebDriverWait 使所需的框架可用并切换到它
    • 诱导 WebDriverWait 使 input 元素 E-mail 可点击。
    • 您可以使用以下解决方案。
    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait 
      from selenium.webdriver.support import expected_conditions as EC
      
      url = 'http://www.valor.com.br'
      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_argument('disable-infobars')
      browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      browser.get(url)
      WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.topo_login>a.login-valor.login-globo"))).click()
      WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='https://login.globo.com/login/6668?url=&tam=WIDGET']")))
      WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-pristine.ng-invalid.ng-invalid-required[name='login']"))).send_keys("teste")
      
    • 浏览器快照:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 2021-07-03
      • 2015-02-04
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      相关资源
      最近更新 更多