JS 在 iframe 中添加值

JS add value in iframe

我正在尝试找到一种在当前 iframe 中添加值以提交和解析 hCaptcha 的方法。

目前我可以使用以下方法获取 iframe:

document.querySelector('[title="widget containing checkbox for hCaptcha security challenge"]')

由于没有id,但是可以通过上面提供的代码定位到iframe。

但是我找不到方法,因为我不是 JS 编码员,无法在 iframe 中输入“data-hcaptcha-response”的值,我需要在提交表单之前输入该值。

完整的 iframe 代码:

<iframe src="xyz" 
 title="widget containing checkbox for hCaptcha security challenge" 
 tabindex="0" frameborder="0" scrolling="no" data-hcaptcha-widget-id="0zlooj2ooak" 
 data-hcaptcha-response="" style="width: 303px; height: 78px; overflow: hidden;"></iframe>

如果有人能指导我做这个项目,我将不胜感激。

我猜这应该可以完成工作:

let hcaptcha = document.querySelector('[title="widget containing checkbox for hCaptcha security challenge"]');
hcaptcha.dataset.hcaptchaResponse = "your_value_here";

您可以在 javascript here 中了解有关使用数据属性的更多信息。

我想你是在要求一个属性的设定值。 试试这个代码,我想这会解决你的问题。

document
.querySelector('[title="widget containing checkbox for hCaptcha security challenge"]')
.setAttribute("data-hcaptcha-response", "value of hcaptcha");