css - XPATH 或 CSS I 我在 span 标签中有一个复选框列表。我正在尝试选择一个特定的复选框

标签 css python-2.7 selenium xpath selenium-webdriver

我在 HTML span 标记中有一个复选框列表。在span标签中,有一个输入标签和一个标签标签。我想单击输入标签中具有标签文本的复选框,例如“允许性别不匹配” 在我的 XPATH 中,我可以到达 label 标签,但我不知道如何向上 1,以便我可以单击输入标签。

我需要一些帮助来获取 XPATH 或 CSS 定位器。

我无法使用 id="gwt-uid-1204",因为这是一个动态值。当您访问该页面时它会发生变化

我获取标签文本的 XPATH 是:

//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]/span/label[contains(text(), "Allow gender mismatch")]

HTML 片段是:

<div id="match_configuration_add_possible_tab_match_rules_fp_flags">
    <div class="gwt-Label matchruleheader">Gender and title flags</div>
    <span class="gwt-CheckBox" style="display: block;">
        <input id="gwt-uid-1204" type="checkbox" value="on" tabindex="0"/>
        <label for="gwt-uid-1204">Gender must be present in both names</label>
    </span>
    <span class="gwt-CheckBox" style="display: block;">
        <input id="gwt-uid-1205" type="checkbox" value="on" tabindex="0"/>
        <label for="gwt-uid-1205">Gender must be consistent in both names</label>
    </span>
    <span class="gwt-CheckBox" style="display: block;">
        <input id="gwt-uid-1206" type="checkbox" value="on" tabindex="0"/>
        <label for="gwt-uid-1206">Allow gender mismatch</label>
    </span>
    <span class="gwt-CheckBox" style="display: block;">
    <span class="gwt-CheckBox" style="display: block;">
    <span class="gwt-CheckBox" style="display: block;">
    -- More checkboxes
</div>

谢谢,里亚兹

最佳答案

你至少有两种可能性:

1) 使用 xpaths ".."向上移动一个元素

//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]/span/label[contains(text(), "Allow gender mismatch")]/../input

2) 使用 xpath“preceding-sibling”:

//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]/span/label[contains(text(), "Allow gender mismatch")]/preceding-sibling::input

我个人更喜欢第一种方法,因为即使同级元素的顺序发生变化,它仍然有效。使用第二种方法,您始终需要检查 sibling 是“前一个”还是“后一个”(然后您必须使用following-sibling)

关于css - XPATH 或 CSS I 我在 span 标签中有一个复选框列表。我正在尝试选择一个特定的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33145632/

相关文章:

ssl - 如何使用 Selenium 检测页面上的 SSL 已损坏

javascript - 动态生成的容器上的 Bootstrap 下拉菜单 z-index

jquery - 为两个相似的元素独立地使用 jQuery 更改 CSS 类

css - 将 div 固定在具有负边距的特定位置,然后在单击按钮时对其进行动画处理。

linux - 使用 nohup 在与 ssh 断开连接时帮助运行 python 代码循环

在 IE 中运行的 Selenium Webdriver 脚本在 Firefox 中挂起

python - 使用 BeautifulSoup 抓取特定的表行

html - textarea 中的插入符不会自动传递新行(仅在 chrome 中)

python - 如何将 utf-8 花式引号转换为中性引号

python - 使用 pyspark 从 Hadoop 中删除文件(查询)