首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Selenium -获取页面中的所有iframe(即使是嵌套的)?

Selenium -获取页面中的所有iframe(即使是嵌套的)?
EN

Stack Overflow用户
提问于 2014-04-22 22:53:00
回答 4查看 14.3K关注 0票数 13

我正在尝试使用selenium webdriver搜索我到达的所有网站的html。在selenium中,当我有一个iframe时,我必须切换到iframe,然后切换回主html来搜索其他iframe。

然而,对于嵌套的iframe,这可能会非常复杂。我必须切换到一个iframe,在其中搜索iframe,然后切换到找到的一个iframe,在it中搜索iframe,然后转到另一个iframe,我必须切换到主框架,然后保存路径以切换回以前所在的位置,依此类推。

不幸的是,我发现很多页面在iframe内都有iframe,iframe内也有iframe(以此类推)。

有没有简单的算法来解决这个问题?或者是一种更好的方法?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-07-26 16:25:43

我找不到一个有多层嵌套框架的网站来完全测试这个概念,但我能够在一个只有一层嵌套框架的网站上测试它。因此,这可能需要一些调试来处理更深层次的嵌套。此外,此代码还假设每个iframe都有一个name属性。

我相信按照这些思路使用递归函数将为您解决这个问题,下面是一个与之配套的示例数据结构:

代码语言:javascript
复制
def frame_search(path):
    framedict = {}
    for child_frame in browser.find_elements_by_tag_name('frame'):
        child_frame_name = child_frame.get_attribute('name')
        framedict[child_frame_name] = {'framepath' : path, 'children' : {}}
        xpath = '//frame[@name="{}"]'.format(child_frame_name)
        browser.switch_to.frame(browser.find_element_by_xpath(xpath))
        framedict[child_frame_name]['children'] = frame_search(framedict[child_frame_name]['framepath']+[child_frame_name])
        ...
        do something involving this child_frame
        ...
        browser.switch_to.default_content()
        if len(framedict[child_frame_name]['framepath'])>0:
            for parent in framedict[child_frame_name]['framepath']:
                parent_xpath = '//frame[@name="{}"]'.format(parent)
                browser.switch_to.frame(browser.find_element_by_xpath(parent_xpath))
    return framedict

您可以通过调用:frametree = iframe_search([])来启动它,framedict最终将如下所示:

代码语言:javascript
复制
frametree = 
{'child1' : 'framepath' : [], 'children' : {'child1.1' : 'framepath' : ['child1'], 'children' : {...etc}}, 
 'child2' : 'framepath' : [], 'children' : {'child2.1' : 'framepath' : ['child2'], 'children' : {...etc}}}

注意:我之所以写这篇文章是为了使用框架的属性来识别它们,而不是仅仅使用find_elements方法的结果,是因为我发现在某些情况下,Selenium会在页面打开太长时间后抛出一个陈旧的数据异常,而这些响应不再有用。显然,框架的属性不会改变,所以使用xpath会更稳定一些。希望这能有所帮助。

票数 8
EN

Stack Overflow用户

发布于 2018-07-08 00:27:21

仅通过HTML元素标签或属性(包括ID)查找iframe似乎不可靠。

另一方面,通过iframe索引进行递归搜索的效果相对较好。

代码语言:javascript
复制
def find_all_iframes(driver):
    iframes = driver.find_elements_by_xpath("//iframe")
    for index, iframe in enumerate(iframes):
        # Your sweet business logic applied to iframe goes here.
        driver.switch_to.frame(index)
        find_all_iframes(driver)
        driver.switch_to.parent_frame()
票数 7
EN

Stack Overflow用户

发布于 2015-06-24 01:16:00

您可以将一个iFrame嵌套到另一个iFrame中,方法是记住要定位的简单代码行,然后使用以下完整代码中的,重新定位光标回到屏幕的同一区域,记住始终将较大的iFrame放在第一位,然后定义较小的iFrame的位置,如以下完整示例所示:

代码语言:javascript
复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Daneiella Oddie, Austrailian Ballet Dancer, dancing to Bach-Gounod's Ave Maria</title>
</head>
<body bgcolor="#ffffcc">

<DIV style="position: absolute; top:0px; left:0px; width:0px; height:0px"></div>
<DIV style="position: absolute; top:10px; left:200px; width:900px; height:500px">

<iframe width="824" height="472" src="http://majordomoers.me/Videos/DanielaOddiDancingToBack_GounodsAveMaria.mp4" frameborder="0" allowfullscreen></iframe>
</div>

<DIV style="position: absolute; top:0px; left:0px; width:0px; height:0px"></div>
<DIV style="position: absolute; top:10px; left:0px; width:50px; height:50px">

<iframe src="http://majordomoers.me/Videos/LauraUllrichSingingBach_GounodsAveMaria.mp4" frameborder="0" allowfullscreen></iframe>

</div>

<DIV style="position: absolute; top:0px; left:0px; width:0px; height:0px"></div>
<DIV style="position: absolute; top:470px; left:10px; width:1050px; height:30px">

<br><font face="Comic Sans MS" size="3" color="red">  
<li><b>Both Videos will START automatically...but the one with the audio will preceed the dancing by about 17 seconds.  You should keep
<li>both videos at the same size as presented here.  In all, just lean back and let it all unfold before you, each in its own time.</li></font>
</div>
<br>

</body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23223018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档