简体   繁体   中英

How to switch between multiple frames using Selenium and Java

I want to send keys to Card Number, Expiration Date and CVV text fields which are in iframe.

Now what I observed is, when in the test case, whichever frame I write first to switch is located and the keys are sent and other two are ignored.

In below code, I mentioned expiration date frame first which is located but the card frame ie cddnumber id frame is not found.

cpp.fillintextfields.get(4).sendKeys("test@test.com");
WebElement es = driver1.findElement(By.id("CollectJSInlineccexp"));
driver1.switchTo().frame(es);
cpp.expdate.sendKeys("01/21");
driver1.switchTo().frame("CollectJSInlineccnumber");
Thread.sleep(2000);
cpp.cdnumber.sendKeys("4111111111111111");
Thread.sleep(5000);

Now when I mention cddnumber ie card number frame first as in code below and expiration date frame after that, card number frame is located and expiration date ones is not located.

cpp.fillintextfields.get(4).sendKeys("test@test.com");
driver1.switchTo().frame("CollectJSInlineccnumber");
Thread.sleep(2000);
cpp.cdnumber.sendKeys("4111111111111111");
Thread.sleep(5000);
WebElement es = driver1.findElement(By.id("CollectJSInlineccexp"));
driver1.switchTo().frame(es);
cpp.expdate.sendKeys("01/21");

Following are the TestNG traces of error given in short when I mention expiry date frame before card number frame:

org.openqa.selenium.NoSuchFrameException: No frame element found by name or id CollectJSInlineccnumber
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'KE5', ip: '10.6.6.105', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '10.0.2'
Driver info: driver.version: unknown
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.frame(RemoteWebDriver.java:885)

Please help me and suggest me any solution to tackle this issue so that all the frames can be located even though I write them one after the other.

When you need to switch between two child frames of the same parent frame (eg Top Level Frame ) you need to switch to the defaultContent which is either the first frame on the page, or the main document when a page contains iframes and then switch to the second child frame as follows:

  • First code block:

     cpp.fillintextfields.get(4).sendKeys("test@test.com"); WebElement es = driver1.findElement(By.id("CollectJSInlineccexp")); driver1.switchTo().frame(es); cpp.expdate.sendKeys("01/21"); driver1.switchTo().defaultContent(); Thread.sleep(2000); driver1.switchTo().frame("CollectJSInlineccnumber"); Thread.sleep(2000); cpp.cdnumber.sendKeys("4111111111111111");
  • Second code block:

     cpp.fillintextfields.get(4).sendKeys("test@test.com"); driver1.switchTo().frame("CollectJSInlineccnumber"); Thread.sleep(2000); cpp.cdnumber.sendKeys("4111111111111111"); Thread.sleep(5000); driver1.switchTo().defaultContent(); WebElement es = driver1.findElement(By.id("CollectJSInlineccexp")); driver1.switchTo().frame(es); cpp.expdate.sendKeys("01/21");

References

You can find a couple of relevant discussions in:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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