简体   繁体   English

如何使用Java在Selenium WebDriver中切换帧

[英]How to switch between frames in Selenium WebDriver using Java

I am using java with WebDriver.I have to switch between two frames. 我正在使用Java与WebDriver.I必须在两帧之间切换。 I have recorded the test case in selenium IDE and in that I got the values as selectFrame relative=top select Frame=middle Frame 我已经在selenium IDE中记录了测试用例,并且我得到的值为selectFrame relative = top select Frame = middle Frame

But there is a problem it is not able to recognize the relative=top and middleFrame. 但是有一个问题是它无法识别relative = top和middleFrame。 How can I solve this problem in Selenium WebDriver with Java? 如何使用Java在Selenium WebDriver中解决此问题?

WebDriver's driver.switchTo().frame() method takes one of the three possible arguments: WebDriver的driver.switchTo().frame()方法接受三个可能的参数之一:

  • A number. 一个号码。

    Select a frame by its (zero-based) index. 按其(从零开始)索引选择一个帧。 That is, if a page has three frames, the first frame would be at index 0 , the second at index 1 and the third at index 2 . 也就是说,如果页面具有三个帧,则第一帧将位于索引0 ,第二帧位于索引1 ,第三帧位于索引2 Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame. 选择框架后,WebDriver界面上的所有后续调用都将进入该框架。

  • A name or ID. 名称或ID。

    Select a frame by its name or ID. 按名称或ID选择框架。 Frames located by matching name attributes are always given precedence over those matched by ID. 通过匹配名称属性定位的帧始终优先于ID匹配的帧。

  • A previously found WebElement . 以前找到的WebElement

    Select a frame using its previously located WebElement. 使用先前定位的WebElement选择框架。

Get the frame by it's id/name or locate it by driver.findElement() and you'll be good. 通过它的id / name获取帧或通过driver.findElement()找到它,你会很好。

to switchto a frame: 切换到一个框架:

driver.switchTo.frame("Frame_ID");

to switch to the default again. 再次切换到默认值。

driver.switchTo().defaultContent();

First you have to locate the frame id and define it in a WebElement 首先,您必须找到帧ID并在WebElement定义它

For ex:- WebElement fr = driver.findElementById("id"); 例如: - WebElement fr = driver.findElementById("id");

Then switch to the frame using this code:- driver.switchTo().frame("Frame_ID"); 然后使用以下代码切换到帧: - driver.switchTo().frame("Frame_ID");

An example script:- 示例脚本: -

WebElement fr = driver.findElementById("theIframe");

driver.switchTo().frame(fr);

Then to move out of frame use:- driver.switchTo().defaultContent();

This code is in groovy, so most likely you will need to do some rework. 这段代码很时髦,所以很可能你需要做一些返工。 The first param is a url, the second is a counter to limit the tries. 第一个参数是一个网址,第二个是限制尝试的计数器。

public boolean selectWindow(window, maxTries) {
    def handles
    int tries = 0
    while (true) {
        try {
            handles = driver.getWindowHandles().toArray()
            for (int a = handles.size() - 1; a >= 0 ; a--) { // Backwards is faster with FF since it requires two windows
                try {
                    Log.logger.info("Attempting to select window: " + window)
                    driver.switchTo().window(handles[a]);
                    if (driver.getCurrentUrl().equals(window))
                        return true;
                    else {
                        Thread.sleep(2000)
                        tries++
                    }
                    if (tries > maxTries) {
                        Log.logger.warn("Cannot select page")
                        return false
                    }
                } catch (Exception ex) {
                    Thread.sleep(2000)
                    tries++
                }
            }
        } catch (Exception ex2) {
            Thread.sleep(2000)
            tries++
        }
    }
    return false;
}

Need to make sure once switched into a frame, need to switch back to default content for accessing webelements in another frames. 需要确保一旦切换到一个帧,需要切换回默认内容以访问另一帧中的webelements。 As Webdriver tend to find the new frame inside the current frame. 因为Webdriver倾向于在当前帧内找到新帧。

driver.switchTo().defaultContent()

You can also use: 您还可以使用:

driver.switch_to.frame(0)

(0) being the first iframe on the html. (0)是html上的第一个iframe。

to switch back to the default content: 切换回默认内容:

driver.switch_to.default_content()

There is also possibility to use WebDriverWait with ExpectedConditions (to make sure that Frame will be available). 也可以将WebDriverWait与ExpectedConditions一起使用(以确保Frame可用)。

  1. With string as parameter 使用字符串作为参数

     (new WebDriverWait(driver, 5)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frame-name")); 
  2. With locator as a parameter 使用定位器作为参数

     (new WebDriverWait(driver, 5)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("frame-id"))); 

More info can be found here 更多信息可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM