栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有没有一种方法可以首先在主窗口中搜索网络元素(如果找不到),然后开始在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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636067.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号