当前位置: 首页 > 面试题库 >

使用Python + Selenium选择iframe

惠凯歌
2023-03-14
问题内容

因此,我对如何在Selenium中做到这一点完全感到困惑,并且无法在任何地方找到答案,所以我分享了自己的经验。

我正在尝试选择一个iframe,但没有运气(或者无论如何也不能重复)。HTML看起来像这样:

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

    @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命令的每种组合都重复失败。偶尔的成功是无法重现的,所以也许是某种竞赛条件或某种东西?从来没有找到正确的方法来获取selenium。


问题答案:

在使用iframe进行测试并尝试在iframe中插入数据时,这对我适用于Python(v。2.7)webdriverSelenium

self.driver = webdriver.Firefox()

## Give time for iframe to load ##
time.sleep(3)
## You have to switch to the iframe like so: ##
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
## Insert text via xpath ##
elem = driver.find_element_by_xpath("/html/body/p")
elem.send_keys("Lorem Ipsum")
## Switch back to the "default content" (that is, out of the iframes) ##
driver.switch_to.default_content()


 类似资料:
  • 问题内容: 现在,我的脚本转到页面,并在出现错误消息之前,从下拉列表“ Vijesti”中打开第二个对象。 这是错误: StaleElementReferenceException:消息:在缓存中找不到元素-自查找页面以来,页面可能已更改 从selenium站点: 当对元素的引用现在“陈旧”时抛出。陈旧意味着元素不再出现在页面的DOM上。StaleElementReferenceException

  • 我已经搜索了前面的一些问题,并没有能够纠正--我是一个完全的新手,所以请原谅我的无知...尝试使用以下方法选择页面上的第三个“单选”按钮: 结果是: “消息:没有此类元素:找不到元素:{”method“:”XPath“,”Selector“:”//[@id=“Wizard_Tabs”]/div/div[1]/div/ul/li[3]/input“}”

  • 问题内容: 我有以下HTML代码 我正在尝试使用Selenium获取选项值的列表(例如459、100等,而不是文本)。 目前,我有以下Python代码 如您所见,代码返回纯HTML,我正在使用HTMLParser库进行解析。有什么方法可以仅使用Selenium来获取选项值?换句话说,不必解析Selenium的结果吗? 问题答案: 检查一下,这是我做的,然后才知道选择模块做了什么 这样的输出

  • 感谢任何帮助!第一次海报和努比编码器:)

  • https://imgur.com/vfhp8n7.png 当我单击B按钮时,整行隐藏 我需要硒来点击B按钮,然后点击下一个B按钮 我想执行,但是数字4352,6820每次都不一样 你有什么想法吗? 编辑

  • 问题内容: 问题: 无法从CSS选择器特定元素中选择。需要验证注册用户是否可以成功更改其密码。我试过了类的不同属性来调用它。当尝试前两个示例时,结果是方法中的异常错误。最后的尝试将调用第一个类实例并重置密码字段(失败)。 尝试过: 目的: 我需要选择共享同一班级的项目。如下所示,该类是共享的。 问题答案: 编辑:因为选择器需要一个,或,但是它们本身都不是。 提供要匹配的类名,并从那里指定要选择的特