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

要求: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。如何处理这样的用例?


任何建议会有所帮助吗?谢谢。


弑天下
浏览 54回答 1
1回答

www说

是的,您可以编写一个循环来遍历所有 iframe,如果元素不存在于主窗口中。实现:&nbsp;if (driver.findElements(By.xpath("xpath goes here").size()==0){&nbsp; &nbsp; &nbsp;int size = driver.findElements(By.tagName("iframe")).size();&nbsp; &nbsp; &nbsp;for(int iFrameCounter=0; iFrameCounter<=size; iFrameCounter++){&nbsp; &nbsp; &nbsp; &nbsp; driver.switchTo().frame(iFrameCounter);&nbsp; &nbsp; &nbsp; &nbsp; if (driver.findElements(By.xpath("xpath goes here").size()>0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("found the element in iframe:" + Integer.toString(iFrameCounter));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // perform the actions on element here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; driver.switchTo().defaultContent();&nbsp; &nbsp; }&nbsp;}蟒蛇实现# 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):&nbsp; &nbsp; # get the number of iframes&nbsp; &nbsp; iframes = driver.find_elements_by_tag_name("iframe")&nbsp; &nbsp; # iterate through all iframes to find out which iframe the required element&nbsp; &nbsp; for iFrameNumber in iframes:&nbsp; &nbsp; &nbsp; &nbsp; # switching to iframe (based on counter)&nbsp; &nbsp; &nbsp; &nbsp; driver.switch_to.frame(iFrameNumber+1)&nbsp; &nbsp; &nbsp; &nbsp; # check if the element present in the iframe&nbsp; &nbsp; &nbsp; &nbsp; if len(driver.finds_element_by_xpath("xpath goes here")) > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("found element in iframe :" + str(iFrameNumber+1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # perform the operation here&nbsp; &nbsp; &nbsp; &nbsp; driver.switch_to.default_content()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java