问题描述
无法对 iframe 使用 send_key().如何选择这个 iframe 以及里面的哪个元素应该用于 send_key()?
Unable to use send_key() for a iframe. How to select this iframe and which element inside this should be use for send_key()?
和 iframe html 代码
and iframe html code
<iframe class="textarea" src="/framework/html/blank.html" style="width: 99%; border-width: 1px; height: 332px;">
#document
<html webdriver="true">
<head>
</head>
<body> … </body>
</html>
</iframe>
如何将值发送到描述?
还有一件事我想知道,当我在浏览器中查看页面源代码"时,这个框架代码不会出现?
One more thing i want to know that this code of frame is not coming when i go for "view page source " in browser?
推荐答案
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
假设驱动程序是一个健康的 webdriver 实例.要继续使用默认内容,请执行 driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
assuming that driver is a healthy instance of webdriver. To continue with the default content do driver.switch_to.default_content()
编辑:当您切换到需要的框架时,像往常一样定位您的网页元素.我猜(但不确定)在你的情况下这将是一个 html/body,所以
EDIT: When you have switched to needed frame, locate your webelement as you always do. I guess (but not sure) in your case this will be a html/body, so
el = driver.find_element_by_xpath('html/body')
应该没问题.并执行
el.send_keys('keys_to_send')
EDIT2:在发送键之前,您可能必须专注于元素(点击应该做的事情,应该出现一个子元素).或者你可以直接通过 JS 放置需要的文本.
EDIT2: Before sending keys you may have to focus on the element (click should do the thing, a child element should appear). Or you can just place the needed text via JS.
driver.execute_script('document.body.innerHTML = "%s"' % text_var)
这篇关于html 中的硒和 iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!