简体   繁体   English

无法在浏览器控制台中使用 javascript select 复选框

[英]Unable to select checkbox using javascript in browser console

i am automating our application using a tool that supports javascript.Navigated to the page at browser console, i am trying to select the checkbox inside a web component using document.queryselector method but i am not able to succeed in checking the box.我正在使用支持 javascript 的工具自动化我们的应用程序。导航到浏览器控制台的页面,我正在尝试使用 document.queryselector 方法 select web 组件内的复选框,但我无法成功选中该框。

tried .checked = true , select and submitform.尝试过.checked = true 、select 和提交表单。

i am relatively new in this company and to the tool, so need some tip.我在这家公司和工具方面相对较新,所以需要一些提示。

附上截图

You are using querySelectorAll() that return an array, so you need to specify the position of the array that you want to change.您正在使用返回数组的querySelectorAll() ,因此您需要指定要更改的数组的 position。 Actually in the image show that the function return an array with 2 elements.实际上在图像中显示 function 返回一个包含 2 个元素的数组。

You can use for loop or need to specify the position.您可以使用 for 循环或需要指定 position。

var checkboxes = document.querySelectorAll('selector');
for (var i = 0, len = checkboxes.length; i < len; i++) {
//work with checkboxes[i].checked = true;
}

First thing you need to check in the DOM (Elements Inspector):首先需要检查 DOM(元素检查器):
Does this Web Component have a shadow-root ??这个 Web 组件是否有shadow-root

If so you need to "go in" with something like:如果是这样,您需要使用以下内容“进入”:
document.querySelector("my-component").shadowRoot.querySelector("....").checked=true

Better yet is to use a DOM dive that dives into shadowRoots:更好的是使用潜入 shadowRoots 的 DOM 潜水:

const shadowDive = (
           el,
           selector,
           match = (el,root)=>{  console.warn('match', el, root); }
)=>{
    let root = el.shadowRoot || el;
    root.querySelector(selector) && match(root.querySelector(selector), root);
    [...root.querySelectorAll("*")].map( el => shadowDive(el, selector, match) );
}
shadowDive( document.body ); // note optional 2nd, 3rd parameter

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

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