简体   繁体   English

如何使用 Selenium 和 Java 在多帧之间切换

[英]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.我想将密钥发送到 iframe 中的卡号、到期日期和 CVV 文本字段。

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.在下面的代码中,我首先提到了到期日期框架,它位于但没有找到卡框架,即 cddnumber id 框架。

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.现在,当我在下面的代码中首先提到 cddnumber 即卡号框架和之后的到期日期框架时,找到卡号框架而没有找到到期日期框架。

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:以下是当我在卡号框架之前提到到期日期框架时,简短给出的 TestNG 错误痕迹:

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:当您需要在同一父框架的两个框架(例如顶级框架)之间切换时,您需要切换到defaultContent上的第一个框架,或者当页面包含 iframe 时的主文档,然后切换到第二个子框架如下:

  • 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:您可以在以下位置找到一些相关的讨论:

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

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