资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 面试经验 > 面试问答

有没有一种方法可以首先在主窗口中搜索网络元素(如果找不到),然后开始在iframe中搜索?

面试问答 更新时间: 发布时间: 计算机考试归档 最新发布

有没有一种方法可以首先在主窗口中搜索网络元素(如果找不到),然后开始在iframe中搜索?

是的,如果主窗口中不存在该元素,则可以编写一个循环遍历所有iframe。 Java实现:

 if (driver.findElements(By.xpath("xpath goes here").size()==0){     int size = driver.findElements(By.tagName("iframe")).size();     for(int iframeCounter=0; iframeCounter<=size; iframeCounter++){        driver.switchTo().frame(iframeCounter);        if (driver.findElements(By.xpath("xpath goes here").size()>0){ System.out.println("found the element in iframe:" + Integer.toString(iframeCounter)); // perform the actions on element here        }        driver.switchTo().defaultContent();    } }

Python实现

# switching to parent window - added this to make sure always we check on the parent window firstdriver.switch_to.default_content()# check if the elment present in the parent windowif (len(driver.finds_element_by_xpath("xpath goes here"))==0):    # get the number of iframes    iframes = driver.find_elements_by_tag_name("iframe")    # iterate through all iframes to find out which iframe the required element    for iframeNumber in iframes:        # switching to iframe (based on counter)        driver.switch_to.frame(iframeNumber+1)        # check if the element present in the iframe        if len(driver.finds_element_by_xpath("xpath goes here")) > 0: print("found element in iframe :" + str(iframeNumber+1)) # perform the operation here        driver.switch_to.default_content()


转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/636067.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【有没有一种方法可以首先在主窗口中搜索网络元素(如果找不到),然后开始在iframe中搜索?】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2