使用 Selenium Webdriver 与 Stripe Card Element iFrame 交互 - Cucumber/Selenium Java
我有一个表单,我想在 Java 中使用 Cucumber 和 Selenium Webdriver 实现自动化——在这个表单中,我们有一个来自 Stripe 的卡片元素.我们调用 div,其余的由 stripe 完成.我不确定这是否是 iFrame,但是当我使用
I have an form that I want to automate using Cucumber and Selenium Webdriver in Java - in this form, we have a card element which we use from Stripe. We call the div, and stripe does the rest. I'm unsure if this is an iFrame, but when I use the
Hooks.driver.findElement(By.xpath("xpathOfTheCardNumberField")).sendKeys("123");
命令,它不与之交互并在控制台日志中返回无法定位元素"错误.
command, it does not interact with it and returns the "Unable to locate element" error in the console log.
我已经要求我们的前端也许尝试在字段中添加一些 ID 或名称标签,但他告诉我他不能与卡片元素内的字段的标记进行交互,只能与卡片元素本身交互 -就像 Stripe 处理其他所有事情一样.
I have asked our front-ender to perhaps try and add some ID's or Name tags to the fields, but he informs me that he cannot interact with the markup for the fields inside of the card element, only the card element itself - as Stripe deal with everything else.
附上一张卡片元素的图片,以及相关卡片元素的标记.
Attached is a picture of the card element, as well as the markup for the card element in question.
是否可以让 Selenium 与此元素交互?
Is it possible to get Selenium to interact with this element?
非常感谢任何帮助.卡片元素前端
标记卡片元素
推荐答案
其实我自己已经想通了,所以我会回答这个问题并关闭它,以防其他人遇到问题.
I've actually figured this out myself, so I'm going to answer the question and close it off in case anyone else is having issues with it.
我认为这是一种通用方法,可用于任何 iframe,而不仅仅是 Stripe.
I think this is a blanket method that can be used for any iframes, not just Stripe.
首先,您必须告诉您的 webdriver 将帧切换到您尝试访问的 iframe:
Firstly, you must tell your webdriver to switch frames to the iframe you are trying to access:
Hooks.driver.switchTo().frame(Hooks.driver.findElement(By.xpath("xpathOfIframe")));
然后,您可以为该 iframe 中的事物创建 webElement,并与它们进行交互:
Then, you can create webElements for the things within that iframe, and interact with them:
WebElement creditcardNumber = Hooks.driver.findElement(By.name("cardnumber"));
creditcardNumber.sendKeys("1234567890000066");
相关文章