Selenium with python 无法本地化 IFRAME 框中的区域文本

问题描述 投票:0回答:1

我正在将 Selenium 与 python 结合使用,并一直尝试单击文本区域并修改消息描述。文本框的 HTML 如下所示:

enter image description here 当我尝试使用此代码时出现错误:

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

我的代码:

iframe = driver.find_element(By.TAG_NAME, "iframe")

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

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

html python-3.x selenium-webdriver textarea
1个回答
0
投票

有两件事:

  1. 上下文可能仍在另一个 IFRAME 上,您已切换该 IFRAME,但忘记切换回主/默认上下文。因此,在切换到此 IFRAME 之前,请先切换到默认上下文,请参阅下面的代码:
# Switch to main frame of the page first
driver.switch_to.default_content()
iframe = driver.find_element(By.TAG_NAME, "iframe")
#switch to selected iframe
driver.switch_to.frame(iframe)
  1. element.sendkeys("new description")
    -- 这是不正确的

    将其更改为以下:

element.send_keys("new description")

注意:是

send_keys
而不是
sendkeys

© www.soinside.com 2019 - 2024. All rights reserved.