Python html 中的硒和 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18924146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:16:21  来源:igfitidea点击:

Selenium and iframe in html

pythoniframeseleniumselenium-webdriver

提问by RATHI

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 使用 send_key()。如何选择这个 iframe 以及里面的哪个元素应该用于 send_key()?

page image

页面图片

and iframe html code

和 iframe html 代码

<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>

How to send the value to description?

如何将值发送到描述?

One more thing i want to know that this code of frame is not coming when i go for "view page source " in browser?

还有一件事我想知道,当我在浏览器中“查看页面源代码”时,这个框架代码不会出现?

采纳答案by Furious Duck

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()

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))假设驱动程序是一个健康的 webdriver 实例。要继续使用默认内容,请执行driver.switch_to.default_content()

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

编辑:当你切换到需要的框架时,像往常一样找到你的 webelement。我猜(但不确定)在你的情况下这将是一个 html/body,所以

el = driver.find_element_by_xpath('html/body')

el = driver.find_element_by_xpath('html/body')

should be fine. And perform

应该没事。并执行

el.send_keys('keys_to_send')

el.send_keys('keys_to_send')

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.

EDIT2:在发送键之前,您可能必须关注元素(点击应该做的事情,应该出现一个子元素)。或者您可以通过 JS 放置所需的文本。

driver.execute_script('document.body.innerHTML = "%s"' % text_var)

driver.execute_script('document.body.innerHTML = "%s"' % text_var)

回答by Furqan Ud Din

In order to handle iframes on a web page you need to switch to the iframes first that are present on the page using this command: driver.switch_to.frame(frame_id)

为了处理网页上的 iframe,您需要首先使用以下命令切换到页面上存在的 iframe:driver.switch_to.frame(frame_id)

This tutorial has a working example of handling iframe using selenium with pythonwhich will made you able to get through this issue.

本教程有一个使用 selenium 和 python 处理 iframe的工作示例,这将使您能够解决这个问题。

回答by Arun211

The Accepted answer works very well for the given question. However I have two reasons for adding one more answer and hope this answer might help someone landing this question with different ask.

接受的答案对于给定的问题非常有效。但是,我有两个理由再添加一个答案,并希望这个答案可以帮助有人提出不同的问题。

Reason 1:

原因一:

Accepted answer has one way of handling iframes inside a web page. However there are different ways to handle iframes.

接受的答案有一种处理网页内 iframe 的方法。但是,处理 iframe 的方法有很多种。

  • By using index position of the frame
  • By using name of the frame
  • By using id of the frame
  • 通过使用框架的索引位置
  • 通过使用框架的名称
  • 通过使用框架的 id

Reason 2:

原因2:

It uses driver.find_element_by_tag_name("iframe")statement to find iframe element which may not work or return expected iframe web element when there are multiple iframe elements in the same page.

它使用driver.find_element_by_tag_name("iframe")语句查找 iframe 元素,当同一页面中有多个 iframe 元素时,这些元素可能不起作用或返回预期的 iframe 网页元素。

There are many articles out there to explain handling iframes in detail.

有很多文章详细解释了如何处理 iframe。

  1. http://allselenium.info/handling-iframes-using-selenium-webdriver/
  1. http://allselenium.info/handling-iframes-using-selenium-webdriver/