Selenium with python Unable to localize area text in IFRAME box

uyhoqukh  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(203)

我正在使用Selenium与python,并一直试图单击文本区域,并修改消息描述。文本框的HTML看起来像这样:


当我尝试使用此代码时出现错误:

  1. selenium.common.exceptions.WebDriverException: Message: TypeError: browsingContext.currentWindowGlobal is null

我的代码:

  1. iframe = driver.find_element(By.TAG_NAME, "iframe")
  2. #switch to selected iframe
  3. driver.switch_to.frame(iframe)
  4. driver.implicitly_wait(5)
  5. element = driver.find_element(By.XPATH, "html/body/p")
  6. element.clear()
  7. element.sendkeys("new description")

尝试在网站上使用Selenium进行测试

hmtdttj4

hmtdttj41#

两件事:
1.上下文可能仍然在另一个IFRAME上,您已经切换,但忘记切换回主/默认上下文。因此,在切换到这个IFRAME之前,请先切换到默认上下文,参见下面的代码:

  1. # Switch to main frame of the page first
  2. driver.switch_to.default_content()
  3. iframe = driver.find_element(By.TAG_NAME, "iframe")
  4. #switch to selected iframe
  5. driver.switch_to.frame(iframe)
  1. element.sendkeys("new description")--不正确
    将其更改为以下内容:
  1. element.send_keys("new description")

注意:它是send_keys而不是sendkeys

相关问题