Selenium 2 - Switching focus to a frame that has n

2020-02-28 06:53发布

So right now I'm trying to figure out how I can switch focus to a frame in Selenium 2 when the frame has no name or id? For a named frame I do:

driver.SwitchTo().Frame(FrameName);

but what is there is no name? Has anyone had experience with this?

5条回答
神经病院院长
2楼-- · 2020-02-28 07:11

In selenium if you know that the relative location of the frame you should be able to use the selectFrame command with the string "relative=up" to move it up a frame e.g. selenium.SelectFrame("relative=up"); or to jump to the top frame use "relative=top"

查看更多
霸刀☆藐视天下
3楼-- · 2020-02-28 07:18

In addition to using the index (as the other answers suggest), in C# you can select the iFrame by tagName. My example assumes there is one and only one iFrame on the page.

try
{
    var iFrameElement = Driver.FindElementByTagName("iFrame");
    var driver = Driver.SwitchTo().Frame(this.iFrameElement);    
    var element = driver.FindElement(selector);

    // do what you need with the element
}
finally
{
    // don't forget to switch back to the DefaultContent
    Driver.SwitchTo().DefaultContent();
}

Note: You have to get the information from the IWebElement .Text or .Click for example, before calling Driver.SwitchTo().DefaultContent();

I created these extensions methods to help

public static IWebDriver SwitchToIFrame(this RemoteWebDriver driver)
{
    // http://computerrecipes.wordpress.com/2012/08/23/selenium-webdriver-interact-with-an-element-inside-an-iframe/
    // http://stackoverflow.com/questions/3549584/selenium-2-switching-focus-to-a-frame-that-has-no-name-id
    var iFrameElement = driver.FindElementByTagName("iFrame");
    return driver.SwitchTo().Frame(iFrameElement);
}

public static void SwitchOutOfIFrame(this IWebDriver driver)
{
    driver.SwitchTo().DefaultContent();
}

An example of using the extensions methods:

public void ClickPrintButton()
{
    var iFrameDriver = Browser.Driver.SwitchToIFrame();
    try
    {
        iFrameDriver.FindElement(By.Id("saveButton")).Click();
    }
    finally
    {
        Browser.Driver.SwitchOutOfIFrame();
    }
}
查看更多
The star\"
4楼-- · 2020-02-28 07:27

You can use the index of the frame. Since you don't have name and id for the frame, driver.switchTo().frame(int frameIndex)

查看更多
Deceive 欺骗
5楼-- · 2020-02-28 07:28

driver.switchTo.frame() is overloaded to accept a frame name or an integer. This int is a 0 based index of the frames available. The first frame would be 0, the second 1 and so on.

I've just run a really quick test using the java binding and Firefox against this HTML page.

<html>
<frameset rows="50%,50%">
    <frame src="frame_a.htm" />
    <frame src="frame_b.htm" />
</frameset>
</html>

I'm successfully able to use driver.switchTo().frame(0); to refer to frame a and driver.switchTo().frame(1); to access frame b.

查看更多
Juvenile、少年°
6楼-- · 2020-02-28 07:28

You can just give the id of your iframe instead of iframe-name.

Please see my below example, it worked for me.
In the example I am switching to one iframe in my page and clicking on the element in that iframe, which is "worksheet0" .

Use the code :

driver.switchTo().frame("topframe");    
    WebElement worksheet0 = driver.findElement(By.xpath("//*@id='reportSelect:Worksheet_lbl']"));               worksheet0.click();             

The HTMLof the iframe :

  < iframe id="topframe" height="83px" frameborder="0" width="100%" scrolling="NO" '1331808552380'"="" +="" src="initialize.do?init=header&cacheBuster=" name="topframe" marginheight="0" marginwidth="0">
查看更多
登录 后发表回答