【问题标题】:Selenium selenium.common.exceptions.NoSuchElementException error sending text to an element within iframeSelenium selenium.common.exceptions.NoSuchElementException 错误将文本发送到 iframe 中的元素
【发布时间】:2022-01-23 01:35:22
【问题描述】:

我是 python 新手,最近进入了 selenium,我从教程中用它为linkedin 或 twitter 做了一些小项目,但现在我想为我的工作(财务)做点什么,我的问题是:

在这个网站上:https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui

当我尝试通过任何选择器(名称 xpath css 选择器等)查找元素时,它告诉我没有这样的元素

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.common.exceptions import NoSuchElementException

URL = 'https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui'


s = Service('My chromedriver path')
driver = webdriver.Chrome(service = s)

driver.get(URL)
driver.maximize_window()
time.sleep(3)
cui_entry = driver.find_element(By.CSS_SELECTOR,'.col-sm-4 p input')
cui_entry.send_keys('23484xxx')

我想要它做的是写这段代码,上面写着 Introduceti codul unic de identificare (numeric): 但我好像做错了什么

【问题讨论】:

  • 您尝试访问的元素位于框架内。您需要切换到框架并执行 sendkeys();

标签: python selenium xpath iframe css-selectors


【解决方案1】:

要将字符序列发送到输入唯一标识码(数字)字段,因为元素位于iframe 中,因此您必须:

  • 诱导WebDriverWait 使所需的框架可用并切换到它

  • 诱导WebDriverWait 使所需的元素可点击

  • 您可以使用以下任一Locator Strategies

    • 使用CSS_SELECTOR

      driver.get("https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#_com_liferay_iframe_web_portlet_IFramePortlet_INSTANCE_AAFALwmoH3eD_iframe")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='cod']"))).send_keys("23484")
      
    • 使用XPATH

      driver.get("https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='_com_liferay_iframe_web_portlet_IFramePortlet_INSTANCE_AAFALwmoH3eD_iframe']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='cod']"))).send_keys("23484")
      
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:


参考

您可以在以下位置找到一些相关讨论:

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 2015-12-25
    • 1970-01-01
    • 2022-01-08
    • 2021-03-24
    • 2021-12-30
    相关资源
    最近更新 更多