Selenium-在一个页面中获取所有iframe(甚至嵌套的iframe)?

2024-05-14 14:28:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

但是,对于嵌套iframe,这可能非常复杂。我必须切换到一个I frame,搜索它的iframe,然后切换到找到的一个iframe,搜索它的iframe,然后转到另一个iframe,我必须切换到主帧,然后保存我的路径以切换回以前的位置,等等

不幸的是,我发现的许多页面都有iframes内的iframes内的iframes(等等)。

有简单的算法吗?还是更好的方法?


Tags: 方法路径算法网站htmlselenium页面frame
3条回答

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

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

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

我相信使用递归函数可以解决这个问题,下面是一个数据结构示例:

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将最终看起来像这样:

frametree = 
{'child1' : 'framepath' : [], 'children' : {'child1.1' : 'framepath' : ['child1'], 'children' : {...etc}}, 
 'child2' : 'framepath' : [], 'children' : {'child2.1' : 'framepath' : ['child2'], 'children' : {...etc}}}

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

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

另一方面,通过iframe索引递归搜索相对来说工作得很好。

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

相关问题 更多 >

    热门问题