当iframe是iframe中的唯一元素时,如何将键发送到iframe

发布于 2025-01-20 10:13:40 字数 2272 浏览 3 评论 0原文

我有这个用于输入卡号的iframe。

我正在以Chrome无头模式尝试

<iframe hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true" marginwidth="0" marginheight="0" 

html inside the iframe with #document
#document
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style>*{margin:0;padding:0;-webkit-box-sizing:border-box;box-sizing:border-box;border:none;background-color:transparent}body,form,input{position:absolute;top:0;left:0;height:100%;width:100%}label{position:absolute;left:-9999px}:focus{outline:0}::-ms-clear,input::-ms-clear{display:none}.autocomplete,.field-description,.focus-helper{opacity:0;pointer-events:none;width:1px;height:1px;left:-1px;z-index:-1}.autocomplete{width:2px;height:2px;left:-2px}</style><style></style></head><body marginwidth="0" marginheight="0"><script src="iframe.min.js"></script><form><input class="focus-helper start" tabindex="0" readonly="" aria-hidden="true" autocomplete="off" autocompletetype="off" x-autocompletetype="off"><input name="number" type="tel" autocorrect="off" autocapitalize="none" spellcheck="false" aria-invalid="false" aria-label="Card number" maxlength="22" autocomplete="cc-number" autocompletetype="cc-number" x-autocompletetype="cc-number" placeholder=" " aria-placeholder=" " class="invalid"><input type="text" name="name" id="name-autocomplete" class="autocomplete" autocomplete="cc-name" autocompletetype="cc-name" x-autocompletetype="cc-name" tabindex="-1" aria-hidden="true"><input type="text" name="expirationMonth" id="expirationMonth-autocomplete" class="autocomplete" autocomplete="cc-exp-month" autocompletetype="cc-exp-month" x-autocompletetype="cc-exp-month" tabindex="-1" aria-hidden="true"><input type="text" name="expirationYear" id="expirationYear-autocomplete" class="autocomplete" autocomplete="cc-exp-year" autocompletetype="cc-exp-year" x-autocompletetype="cc-exp-year" tabindex="-1" aria-hidden="true"><input class="focus-helper end" tabindex="0" readonly="" aria-hidden="true" autocomplete="off" autocompletetype="off" x-autocompletetype="off"></form></body></html>

I have this iframe which is for inputting the card number.

I am trying this in chrome headless mode

<iframe hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true" marginwidth="0" marginheight="0" 

html inside the iframe with #document
#document
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style>*{margin:0;padding:0;-webkit-box-sizing:border-box;box-sizing:border-box;border:none;background-color:transparent}body,form,input{position:absolute;top:0;left:0;height:100%;width:100%}label{position:absolute;left:-9999px}:focus{outline:0}::-ms-clear,input::-ms-clear{display:none}.autocomplete,.field-description,.focus-helper{opacity:0;pointer-events:none;width:1px;height:1px;left:-1px;z-index:-1}.autocomplete{width:2px;height:2px;left:-2px}</style><style></style></head><body marginwidth="0" marginheight="0"><script src="iframe.min.js"></script><form><input class="focus-helper start" tabindex="0" readonly="" aria-hidden="true" autocomplete="off" autocompletetype="off" x-autocompletetype="off"><input name="number" type="tel" autocorrect="off" autocapitalize="none" spellcheck="false" aria-invalid="false" aria-label="Card number" maxlength="22" autocomplete="cc-number" autocompletetype="cc-number" x-autocompletetype="cc-number" placeholder=" " aria-placeholder=" " class="invalid"><input type="text" name="name" id="name-autocomplete" class="autocomplete" autocomplete="cc-name" autocompletetype="cc-name" x-autocompletetype="cc-name" tabindex="-1" aria-hidden="true"><input type="text" name="expirationMonth" id="expirationMonth-autocomplete" class="autocomplete" autocomplete="cc-exp-month" autocompletetype="cc-exp-month" x-autocompletetype="cc-exp-month" tabindex="-1" aria-hidden="true"><input type="text" name="expirationYear" id="expirationYear-autocomplete" class="autocomplete" autocomplete="cc-exp-year" autocompletetype="cc-exp-year" x-autocompletetype="cc-exp-year" tabindex="-1" aria-hidden="true"><input class="focus-helper end" tabindex="0" readonly="" aria-hidden="true" autocomplete="off" autocompletetype="off" x-autocompletetype="off"></form></body></html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2025-01-27 10:13:40

为该xpathiframe写下。正如您提到的

我有这个用于输入卡号的iframe。

因此,对于卡号有一个属性。

//iframe[@attribute='value']

例如:

driver.switchTo().frame("iframe");

WebElement ele = driver.findElement(By.xpath("//iframe[@attribute='value']"));

ele.sendKeys('Text');

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@attribute='value']")));

WebElement inputtext = driver.findElement(By.xpath("//input[@attribute='value']"));

inputtext.sendKeys("text");

希望此答案您的问题,如果这没有回答您的问题,请分享“检查元素”中的HTML。

Write the Xpath for that iframe like the below. As you mentioned

I have this iframe which is for inputting the card number.

so there is an attribute for for card number.

//iframe[@attribute='value']

Eg:

driver.switchTo().frame("iframe");

WebElement ele = driver.findElement(By.xpath("//iframe[@attribute='value']"));

ele.sendKeys('Text');

or

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@attribute='value']")));

WebElement inputtext = driver.findElement(By.xpath("//input[@attribute='value']"));

inputtext.sendKeys("text");

Hope this answer your question, please share the HTML from inspect element if this does not answer your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文