selenium - 如何根据同级输入的@id选择跨度

标签 selenium testing xpath

我有下面的标签,这是多个选择选项按钮的标签,每个选择都有它的div,里面有一个标签,然后在该标签内有一个ID,我需要用它来确定我想要哪个 Selenium 点击。

<label class="custom-toggle-checkbox-label">
   <input type="checkbox" class="custom-toggle-checkbox-input" id="selectOption_1_Value" 
   value="false">
<span class="custom-toggle-checkbox-slider"></span>
</label>


<label class="custom-toggle-checkbox-label">
       <input type="checkbox" class="custom-toggle-checkbox-input" id="selectOption_2_Value" 
       value="false">
    <span class="custom-toggle-checkbox-slider"></span>
    </label>
.....

我只是尝试过,但该元素在这一点上不可交互。我需要单击 span 元素才能选择位于 label 元素下的它

xpath = //*[@id='selectOption_1_Value']

我还尝试使用元素的完整 xpath,但有多个具有相同类名的标签,我想避免使用完整的 xpath 定位器。

我的问题是如何根据子 ID 选择正确的标签?

最佳答案

如果您想根据同级input@id选择span,请尝试

//label[input[@id='selectOption_1_Value']]/span

//input[@id='selectOption_1_Value']/following-sibling::span

关于selenium - 如何根据同级输入的@id选择跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66210932/

相关文章:

testing - 我什么时候应该通过手动创建 "stub"版本而不是使用模拟框架来 stub 类型

testing - Cypress :如何在测试的所有测试开始之前运行一次代码

ReactJS 应用程序 - 弹性 VS 快速失败

javascript - CasperJS 无法找到元素,即使我可以在开发人员工具中按 CTRL-F 组合键

xml - 如何使用xPath和XmlNamespaceManager正确查询xml片段

java - Selenium Webdriver - Opera - 无法从渲染器接收消息

python - 为什么 Selenium 不点击?(Python)

python - 使用selenium从浏览器解析类

java - 我如何在没有物理键盘的情况下用 Selenium 按下键盘键

python - Selenium通过Python使用find_element时找不到元素