18910140161

JavaScript-QuerySelector在HTML DOM中返回空白节点-堆栈溢出

顺晟科技

2022-10-18 13:47:37

185

我有一个HTML文档,其中包含以下多次出现

的标记
<代码><;DIV_ngContent-mjy-c67=";";UIPath_自定义_ID=";13";>;<;DIV_ngContent-mjy-c67=";";类别=";col-XS-12 col-SM-6 col-MD-4 col-LG-3";UIPath_自定义_ID=";12";>;<;按钮_NGCONTENT-MJY-C67=";";角色=“按钮”数据切换=";工具提示";数据放置=";顶部";类=";btn btn-default flag-item";title data-original-title=";允许登录主工具。";UIPath_自定义_ID=";11";>;<;span_NGContent-MJY-C67=";";类=";Glyphicon Glyphicon-确定:之前:<;/span>;<;!-->;";管理员";<;/按钮>;<;!-->;<;/DIV>;<;!-->;<;/DIV>;
文档中总共

有112个按钮,

<代码>类=";BTN BTN-默认标志-项目";

我想选择一个特定的按钮

<div _ngcontent-mjy-c67="" uipath_custom_id="13">
    <div _ngcontent-mjy-c67="" class="col-xs-12 col-sm-6 col-md-4 col-lg-3" uipath_custom_id="12">
        <button _ngcontent-mjy-c67="" role="button" data-toggle="tooltip" data-placement="top" class="btn btn-default flag-item" title data-original-title="Allowed to log into the Main Tools." uipath_custom_id="11">
            <span _ngcontent-mjy-c67="" class="glyphicon glyphicon-ok">
                ::before::
            </span>
            <!---->
            "Admin" 
        </button>
        <!---->
    </div>
    <!---->
</div>

使用JavaScript并单击按钮。

我曾尝试使用QuerySelectorAll,但找不到解决方案。

class="btn btn-default flag-item"
上面

的代码返回所有112个元素,但如何使用选择器只选择按钮

<div _ngcontent-mjy-c67="" uipath_custom_id="13">
    <div _ngcontent-mjy-c67="" class="col-xs-12 col-sm-6 col-md-4 col-lg-3" uipath_custom_id="12">
        <button _ngcontent-mjy-c67="" role="button" data-toggle="tooltip" data-placement="top" class="btn btn-default flag-item" title data-original-title="Allowed to log into the Main Tools." uipath_custom_id="11">
            <span _ngcontent-mjy-c67="" class="glyphicon glyphicon-ok">
                ::before::
            </span>
            <!---->
            "Admin" 
        </button>
        <!---->
    </div>
    <!---->
</div>
归属?

也试过使用

 document.querySelectorAll('button.btn.btn-default.flag-item')

它也返回112个元素,但无法使用QuerySelectorAll对其进行进一步处理。

注意:我需要从UIPath调用这个脚本,所以一个-liner脚本非常受欢迎。

使用我在Chrome中找到

的复制JS路径
data-original-title="Allowed to log into the Main Tools."

它工作得很好,但我不知道这条路径是否总是正确的,因为我试图自动化的页面是一个动态页面,元素是动态创建的。因此,如果此路径适用于某个用户,则此路径可能对其他用户无效。

请帮助.


顺晟科技:

通过使用属性选择器:

document.getElementsByClassName('btn btn-default flag-item')
<代码><;DIV_ngContent-mjy-c67=“ ” uiPath_自定义_ID=“ 13 ”>;<;DIV_ngContent-mjy-c67=“ ” Class=“ col-xs-12 col-sm-6 col-MD-4 col-LG-3 ” UIPath_自定义_ID=“ 12 ”>;<;按钮_ngContent-mjy-c67=“ ” Role=“ Button ” data-toggle=“ Tooltip ” data-placement=“ Top ” Class=“ btn btn-default flag-item ” title data-original-title=“允许登录主工具。” uiPath_自定义_ID=“ 11 ”>;<;span_ngContent-mjy-c67=“ ” Class=“ Glyphicon Glyphicon-OK ”>;:之前:<;/span>;<;!-->;“管理员”<;/按钮>;<;!-->;<;/DIV>;<;!-->;<;/DIV>;

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航