我正在将 Selenium 与 python 结合使用,并一直尝试单击文本区域并修改消息描述。文本框的 HTML 如下所示:
当我尝试使用此代码时出现错误:
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 进行测试
有两件事:
# 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)
element.sendkeys("new description")
-- 这是不正确的
将其更改为以下:
element.send_keys("new description")
注意:是
send_keys
而不是 sendkeys