为了账号安全,请及时绑定邮箱和手机立即绑定

有没有办法先在主窗口中搜索网络,如果没有找到,然后开始在iframes内部搜索?

有没有办法先在主窗口中搜索网络,如果没有找到,然后开始在iframes内部搜索?

弑天下 2022-09-22 10:09:42
要求:Bydefault,在主窗口上搜索网络设备,如果发现执行操作,则在 iframe 内搜索网络设备并执行所需的操作硒 3.141'''WebElement el = driver.findElement(By.xpath("//*[contains(text(),'here')]"));    boolean displayFlag = el.isDisplayed();    if(displayFlag == true)    {    sysout("element available in main window")      el.click();    }    else     {      for(int f=0;f<10;f++)      {          sysout("element available in frameset")            switchToFrame(frameName[f]);          el.click();          System.out.println("Webelement not displayed");      }    }'''我的脚本在第一行本身就失败了。它试图在主窗口中查找元素,但元素实际上在iframe中可用。但要求是首先在主窗口中搜索,然后仅导航到 iframe。如何处理这样的用例?任何建议会有所帮助吗?谢谢。
查看完整描述

1 回答

?
www说

TA贡献1775条经验 获得超8个赞

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


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

    }

 }

蟒蛇实现


# switching to parent window - added this to make sure always we check on the parent window first

driver.switch_to.default_content()


# check if the elment present in the parent window

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


查看完整回答
反对 回复 2022-09-22
  • 1 回答
  • 0 关注
  • 51 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信