Xpath preceding

huangapple go评论77阅读模式
英文:

Xpath preceding

问题

DOM树

<span lightning-radiogroup_radiogroup="" class="slds-radio_faux"></span>
<span lightning-radiogroup_radiogroup="" class="slds-form-element__label">Yes</span></label>

我的XPath

//Label[text()='External']//following::fieldset//span[text()='Yes']

UI设计

Xpath preceding

我想要点击上述UI中的单选按钮,我的XPath已经准确定位到了'Yes'或'No',但遗憾的是无法点击单选按钮。

从我的XPath如何根据标签值(Yes、no)选择单选按钮。

英文:

DOM tree

&lt;span lightning-radiogroup_radiogroup=&quot;&quot; class=&quot;slds-radio_faux&quot;&gt;&lt;/span&gt;
&lt;span lightning-radiogroup_radiogroup=&quot;&quot; class=&quot;slds-form-element__label&quot;&gt;Yes&lt;/span&gt;&lt;/label&gt;

My XPath

//Label[text()=&#39;External&#39;]//following::fieldset//span[text()=&#39;Yes&#39;]

UI design

Xpath preceding

I want to click the radio button from the above UI, my XPath perfectly located the 'Yes' or 'No'
but unfortunately unable to click the radio button.

From my XPath how can I select the radio button based on the label value (Yes, no)

答案1

得分: 3

问题在于您正在选择单选按钮内部的跨度(span)。

您需要选择单选按钮节点。节点类似于这样:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->

<input type="radio" name="name" value="NO">
<label>
    <span lightning-radiogroup_radiogroup="" class="slds-radio_faux">NO</span>
</label>

<input type="radio" name="name2" value="Yes">
<label>
    <span lightning-radiogroup_radiogroup="" class="slds-form-element__label">Yes</span>
</label>

<!-- end snippet -->

因此,您需要类似于以下的 XPath:

//input[@type="radio"]/span[text()="YES"]/preceding::input
英文:

The problem is that you are selecting the span inside a radio button.

You need to select the radio-button node.
It would be a node similar to this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;input type=&quot;radio&quot; name=&quot;name&quot; value=&quot;NO&quot; &gt;
&lt;label&gt;
    &lt;span lightning-radiogroup_radiogroup=&quot;&quot; class=&quot;slds-radio_faux&quot;&gt;NO&lt;/span&gt;
 &lt;/label&gt;

 &lt;input type=&quot;radio&quot; name=&quot;name2&quot; value=&quot;Yes&quot;&gt;
 &lt;label&gt;
    &lt;span lightning-radiogroup_radiogroup=&quot;&quot; class=&quot;slds-form-element__label&quot;&gt;Yes&lt;/span&gt;
 &lt;/label&gt;

<!-- end snippet -->

So you need an XPath like this:

//input[@type=&quot;radio&quot;]/span[text()=&quot;YES&quot;]/preceding::input

答案2

得分: 1

找到包含文本“Yes”的span元素,然后找到其祖先元素,即label,然后通过路径/ancestor::label/input访问其内部的input元素。

"//span[contains(text(), 'Yes')]/ancestor::label/input"

这是示例HTML:

Xpath preceding

英文:

Find the Yes text span and then find it's ancestor element here it is label then go to its input element by /ancestor::label/input

&quot;//span[contains(text(), &#39;Yes&#39;)]/ancestor::label/input&quot;

this is sample HTML:
Xpath preceding

答案3

得分: -1

尝试

//span[.=&#39;是&#39;]/preceding-sibling::span[@class=&#39;slds-radio_faux&#39;]

或者(最有可能的)

//span[.=&#39;是&#39;]/preceding::input[@type=&#39;radio&#39;]
英文:

Try

//span[.=&#39;Yes&#39;]/preceding-sibling::span[@class=&#39;slds-radio_faux&#39;]

or (most likely)

//span[.=&#39;Yes&#39;]/preceding::input[@type=&#39;radio&#39;]

huangapple
  • 本文由 发表于 2020年9月8日 16:23:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63789960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定