简体   繁体   English

识别没有 ID 值的 DOM 元素

[英]Identifying a DOM element without an ID value

I regularly use a web app from Invisalign® to set up orthodontic cases.我经常使用 Invisalign® 的网络应用程序来设置正畸案例。 I use Keyboard Maestro to make keyboard shortcuts within the app by activating JavaScript.我使用 Keyboard Maestro 通过激活 JavaScript 在应用程序中创建键盘快捷键。

An example of the JavaScript triggered by keyboard maestro:键盘大师触发的 JavaScript 示例:

document.getElementById('myButton').click();

The web app was recently updated and there is no longer a button ID. Web 应用程序最近已更新,不再有按钮 ID。 Thus, the getElementByID is no longer functioning.因此,getElementByID 不再起作用。

I have tried the following but it does not work:我已经尝试了以下但它不起作用:

document.getElementByClass('myClass').click();

(I have tried c01143 and c0012) (我试过 c01143 和 c0012)

An example of the HTML for the page is:该页面的 HTML 示例如下:

<div class='c0012'>
        <button type="button" tabindex="-1" data-qa-name="lower" data-qa-toggled="false" class="c0180 c01149 c01151">
            <div class="c01143">
                <svg class="c0189 c0191" viewBox="0 0 24 24"><path d="M2,13 L22,13 L22,16 L2,16 L2,13 Z M8,8 L16,8 L16,11 L8,11 L8,8 Z"></path></svg>
            </div>
        <span class="c01142">Lower</span>
    </button>
</div>

I have also tried:我也试过:

document.getElementBydata-qa-name('lower').click();

That also doesn't work.那也行不通。 Hopefully someone can help out with what might be a very simple solution.希望有人可以帮助解决可能是一个非常简单的解决方案。 Although I know some HTML and CSS my JavaScript is almost nonexistent.虽然我知道一些 HTML 和 CSS,但我的 JavaScript 几乎不存在。

The non-descriptive class names (eg, c0012 , c01143 ) strongly indicate that, somewhere in the site's stack, the HTML/CSS/JS is being dynamically compiled/generated.非描述性的类名(例如, c0012c01143 )强烈表明,在站点堆栈的某个地方,正在动态编译/生成 HTML/CSS/JS。 As such, you should expect that these identifiers could change drastically and quite frequently & you shouldn't rely on them in your snippet.因此,您应该预料到这些标识符可能会发生剧烈且频繁的变化,并且您不应在代码片段中依赖它们。


Method #1 - document.querySelector() using data-qa-name方法 #1 - 使用data-qa-name document.querySelector()

You can use the document.querySelector() method to identify based on the button tag, the button type value, as well as the custom data-qa-name attribute.您可以使用document.querySelector()方法根据button标签、 button type值以及自定义data-qa-name属性进行识别。 The following demonstrates that this method works to identify the element without relying on the class name or the (no longer-existent) ID value:下面演示了此方法可以在不依赖类名或(不再存在的)ID 值的情况下识别元素:

 (() => { console.log(document.querySelector("button[type='button'][data-qa-name='lower']")); })();
 <div class='c0012'> <button type="button" tabindex="-1" data-qa-name="lower" data-qa-toggled="false" class="c0180 c01149 c01151"> <div class="c01143"> <svg class="c0189 c0191" viewBox="0 0 24 24"><path d="M2,13 L22,13 L22,16 L2,16 L2,13 Z M8,8 L16,8 L16,11 L8,11 L8,8 Z"></path></svg> </div> <span class="c01142">Lower</span> </button> </div>

You would lift and shift the above by simply calling click() on the identified element, as you had previously:您可以通过简单地在标识的元素上调用click()来提升和移动上面的内容,就像您之前所做的那样:

document.querySelector("button[type='button'][data-qa-name='lower']").click();

Method #2 - document.querySelectorAll() , filtered using .innerText value方法 #2 - document.querySelectorAll() ,使用.innerText值过滤

While from what you've posted there isn't any strong indication that this data-qa-name attribute would change in either name or value, you can also target the element based on its innerText value similar to the following:虽然从您发布的内容来看,没有任何强烈迹象表明此data-qa-name属性会在名称或值上发生变化,但您也可以根据类似于以下内容的innerText值来定位元素:

[...document.querySelectorAll("button[type='button']")].find(function (ele) {return ele.innerText === "Lower"})).click();

The following snippet, while it appears succinct, performs the following steps:以下代码段虽然看起来很简洁,但执行以下步骤:

  1. Enumerates all button elements with an attribute type of button into an Array structure.将所有属性typebutton button元素枚举到一个 Array 结构中。
  2. Finds and returns the first element in the array that has an innerText property equal to the string Lower .查找并返回数组中第一个元素的innerText属性等于字符串Lower
  3. Executes the click() method on the identified button.在标识的按钮上执行click()方法。

The only scenario in which this particular snippet wouldn't work to your requirements would be if there are buttons appearing somewhere in the DOM tree before the intended button element that also have an innerText property equal to the string "Lower".特定代码段无法满足您的要求的唯一情况是,如果按钮出现在 DOM 树中预期按钮元素之前的某处,并且该按钮也具有与字符串“Lower”相等的innerText属性。 You'll have to perform a small amount of testing to confirm which of these snippets will work most consistently in your scenario.您必须执行少量测试以确认这些代码段中的哪一个在您的场景中最能一致地工作。


Further reading进一步阅读

Considering your self-professed limited understanding of JavaScript, you may wish to consult the following MDN documentation pages to get a better grasp on the methods and linguistic conventions leveraged above:考虑到您自称对 JavaScript 的理解有限,您可能希望查阅以下 MDN 文档页面,以更好地掌握上述方法和语言约定:

With document.querySelector() you can use CSS style selectors to target an element.使用document.querySelector()你可以使用 CSS 样式选择器来定位一个元素。 In the example below it select the first button inside the element with the .c0012 class.在下面的示例中,它选择具有.c0012类的元素内的第一个按钮。

document.querySelector('.c0012 button').click();

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

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