如何使用Selenium和Java在多个帧之间切换 [英] How to switch between multiple frames using Selenium and Java

查看:63
本文介绍了如何使用Selenium和Java在多个帧之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将密钥发送到iframe中的卡号",到期日期"和"CVV"文本字段.

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.

在下面的代码中,我首先提到了到期日期框架,但是该框架未找到卡框架,即cddnumber id框架.

In below code, I mentioned expiration date frame first which is located but the card frame i.e. 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);

现在,当我提到cddnumber时,即首先在下面的代码中提到卡号框架,然后再在其后的失效日期框架中,则找到卡号框架,而未找到失效日期的框架.

Now when I mention cddnumber i.e. 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");

以下是当我在卡号帧之前提到失效日期帧时简短给出的TestNG错误痕迹:

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 (e.g. 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");
    

  • 您可以在以下位置找到一些相关的讨论:

    You can find a couple of relevant discussions in:

    这篇关于如何使用Selenium和Java在多个帧之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆