使用Python + Selenium选择iframe [英] Select iframe using Python + Selenium

查看:58
本文介绍了使用Python + Selenium选择iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对如何在Selenium中做到这一点感到困惑,并且找不到任何答案,所以我分享自己的经验.

So, I was absolutely baffled as to how to do this in Selenium, and couldn't find the answer anywhere, so I'm sharing my experience.

我正在尝试选择一个iframe,但没有运气(或者无论如何也无法重复).HTML是:

I was trying to select an iframe and having no luck (or not repeatably anyway). The HTML is:

<iframe id="upload_file_frame" width="100%" height="465px" frameborder="0" framemargin="0" name="upload_file_frame" src="/blah/import/">
<html>
    <body>
        <div class="import_devices">
            <div class="import_type">
                <a class="secondary_button" href="/blah/blah/?source=blah">
                    <div class="import_choice_image">
                        <img alt="blah" src="/public/images/blah/import/blah.png">
                    </div>
                    <div class="import_choice_text">Blah Blah</div>
                </a>
            </div>
        </div>
    </body>
</html>

Python代码(使用selenium库)正尝试使用以下方法找到此iframe:

The Python code (using the selenium library) was trying to find this iframe using this:

    @timed(650)
def test_pedometer(self):
    sel = self.selenium
    ...
    time.sleep(10)
    for i in range(5):
        try:
            if sel.select_frame("css=#upload_file_frame"): break
        except: pass
        time.sleep(10)
    else: self.fail("Cannot find upload_file_frame, the iframe for the device upload image buttons")

对于我可以找到的Selenium命令的每一个组合,重复都失败.

Repeated fails with every combination of Selenium commands I could find.

偶尔的成功是无法重现的,所以也许是某种种族状况或其他原因?从来没有找到正确的方法来正确地使用硒.

The occasional success would not be reproducible, so perhaps it was some sort of race condition or something? Never did find the right way to get it in selenium proper.

推荐答案

最终对我有用的是:

        sel.run_script("$('#upload_file_frame').contents().find('img[alt=\"Humana\"]').click();")

基本上,不要使用硒在iframe中找到链接并单击它;使用jQuery.Selenium显然有能力运行任意一段javascript(这是python-selenium,我猜最初的selenium命令是runScript之类的),一旦我可以使用jQuery,我就可以执行以下操作:

Basically, don't use selenium to find the link in the iframe and click on it; use jQuery. Selenium has the capability to run an arbitrary piece of javascript apparently (this is python-selenium, I am guessing the original selenium command is runScript or something), and once I can use jQuery I can do something like this: Selecting a form which is in an iframe using jQuery

这篇关于使用Python + Selenium选择iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆