How to select a frame using selenium?

前端 未结 5 567
清酒与你
清酒与你 2020-12-19 16:40

I\'m using Java to create selenium test cases. My system is based on portlets connected to each other. I\'m using \"selectFrame\" command to select the portlet.

I tr

相关标签:
5条回答
  • 2020-12-19 16:47

    I was able to select an "src" frame with no name or ID using this method & Python Selenium. I found the xpath of the element, and did this code to get selenium to select the frame properly (using Python 2.7):

    driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="Detail-innerCT"]/iframe'))
    
    0 讨论(0)
  • 2020-12-19 17:02

    We can give frame name, id, index and WebElement locator for identification

    Syntax:-

    driver.switchTo().frames(); // Switch to window to frame
    
    driver.switchTo().defaultContent();  // Switch to Frame to window
    

    If we know the total number of frames on the web page, then we can use “ index”. Index values help to easily switch between frames. An index will start from Zero i.e. if a web page has only one frame then its index will be Zero. If we don’t know the number of frames we can use “findElementBytabname()” method

    Syntax: -

       try
    {
       driver.switchTo().frame(indexnumber);
    }
      catch(NoSuchFrameException e)
    {
      System.out.println(e.getMessage());
    }
    

    We have use try and catch if now frame will not available this throw exception NoSuchFrameException()

    Use name as locater to find frame Syntax: -

     try
    {
      driver.switchTo().frame(“frameName”);
    }
      catch(NoSuchFrameException e)
    {
      System.out.println(e.getMessage());
    }
    

    Use WebElement for switching frame

    Syntax: -

      try
    {
      WebElement button=driver.findElement(By.xpath(""));
      driver.switchTo().frame(button);
    }
      catch (NoSuchFrameException e)
    {
      System.out.println(e.getMessage());
    }
    
    0 讨论(0)
  • 2020-12-19 17:07

    You have an XPath expression that is supposed to get you the IFrame element you need. However you are not telling Selenium it's an XPath expression. The below is what you need:

    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]"));
    

    Note, my Java is not it's best, so this may cause compilation issues but you should see the idea.

    Find the element first, by telling Selenium it's an XPath expression you are giving it, then use that element and stick it right in the 'switch to frame' expression.

    0 讨论(0)
  • 2020-12-19 17:07
    driver.switchTo().defaultContent();
    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]")));
    
    0 讨论(0)
  • 2020-12-19 17:09

    You can switch to a frame, using its name or id easily:

    driver.switchTo().frame("frame_name");
    

    When you have selected a frame to switch to another frame you have to switch to the parent or root first with something like:

    driver.switchTo().defaultContent();
    driver.switchTo().frame("other_frame_name");
    
    0 讨论(0)
提交回复
热议问题