Applescript:点击ID不明的野生动物园的Javascript

时间:2019-05-04 09:54:10

标签: javascript applescript

如何单击下面列出的“详细”按钮?

<span data-segment-id="bererf44b-be45-4514-922a-0fff0585gb">
    <input type="radio" id="dccd7242-38d2-4f54-4841-73c4b48a2f56-view-selector-bererf44b-be45-4514-922a-0fff0585gb" name="dccd7242-38d2-4f54-4841-73c4b48a2f56-view-selector" value="detailed">
    <label for="dccd7242-38d2-4f54-4841-73c4b48a2f56-view-selector-bererf44b-be45-4514-922a-0fff0585gb">Detailed</label>
</span>

这项工作,但我必须手动输入加密的ID

tell application "Safari Technology Preview" to ¬
    do JavaScript ¬
        "document.querySelectorAll(\"[for=\\\"dccd7242-38d2-4f54-4841-73c4b48a2f56-view-selector-bererf44b-be45-4514-922a-0fff0585gb\\\"]\")[0].click();" in current tab of window 1

更新:

大多数HTML是动态的,我对此不太满意。

<div class="center">
            <div class="segmented-control view-selector component" id="THIS_IS_DYNAMIC">
    <input type="radio" id="THIS_IS_DYNAMIC" name="THIS_IS_DYNAMIC" value="basic">
    <label for="THIS_IS_DYNAMIC">Basic</label>
</span><span data-segment-id="THIS_IS_DYNAMIC">
    <input type="radio" id="THIS_IS_DYNAMIC" name="THIS_IS_DYNAMIC" value="detailed">
    <label for="THIS_IS_DYNAMIC">Detailed</label>
</span></div>
        </div>

2 个答案:

答案 0 :(得分:1)

据我所知,您想触发对<label元素的点击。 您的主要问题是必须手动输入加密的ID。

建议您搜索值为<label的{​​{1}}元素。

'Detailed'

编辑:将[...document.querySelectorAll('label')].find(x=>x.innerText=='Detailed' || x.textContent == 'Detailed').click(); //triggers onclick // or use Array.from(document.querySelectorAll('label')).find(x=>x.innerText=='Detailed' || x.textContent == 'Detailed').click() // hint. document.querySelectorAll doesn't return a normal array but a nodelist 元素更改为<input元素。

答案 1 :(得分:0)

好的,很抱歉我的问题一开始并不清楚,但是很遗憾,我也不能使用标签(也不能使用动态标签),但是我发现可以解决<div class="segmented-control view-selector component"

tell application "Safari Technology Preview" to ¬
    do JavaScript ¬
        "document.querySelectorAll(\"[value=\\\"detailed\\\"]\")[0].click();" in current tab of window 1