java - Selenium WebDriver Java - 按标签单击元素不适用于某些标签

标签 java selenium selenium-webdriver webdriver

我正在尝试单击标签旁边的元素。这是我正在使用的代码:

driver.findElement(By.xpath("id(//label[text() = 'LABEL TEXT HERE']/@for)")).click();

它适用于(全选)和 Hayward,但找不到洛杉矶、旧金山或圣何塞。

更新:

目前我想这可能是我最好的选择,直到我看到更好的东西。这将允许用户传递完整的字符串,并且该方法中的函数将抓取字符串的最后一个单词并将其插入到 contains xpath 中。

public void subStringLocationTest(String location) {

    String par = location.substring(location.lastIndexOf(" ") + 1);

    driver.findElement(By.xpath("//label[contains(text(), '" + par + "')]")).click();

}

这是 HTML 代码:

<div id="ReportViewer1_ctl04_ctl03_divDropDown" onclick="event.cancelBubble=true;" onactivate="event.cancelBubble=true;" style="border: 1px solid rgb(169, 169, 169); font-family: Verdana; font-size: 8pt; overflow: auto; background-color: window; display: inline; position: absolute; z-index: 11; left: 131px; top: 41px; width: 188px;">
    <span><table cellpadding="0" cellspacing="0" style="background-color:window;">
        <tbody><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl00" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl00" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnSelectAllClick(this);"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl00">(Select All)</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl02" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl02" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl02">Hayward</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl03" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl03" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl03">Los Angeles</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl04" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl04" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl04">San Francisco</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl05" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl05" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl05">San Jose</label></span></td>
        </tr>
    </tbody></table><input type="hidden" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl01$HiddenIndices" id="ReportViewer1_ctl04_ctl03_divDropDown_ctl01_HiddenIndices" value=""></span>
</div>

最佳答案

您可以使用 xpath 定位器。例如:

d.findElement(By.xpath("//...../label[contains(@for,'ReportViewer1_ctl04_ctl03_divDropDown_ctl02')]")).click();

关于java - Selenium WebDriver Java - 按标签单击元素不适用于某些标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23704518/

相关文章:

java - Spring 休息模板

Java + spring - 将历史事件持久保存到数据库日志的中心方式

python - 使用 Chromedriver 设置 Chrome 的下载首选项 (Python)

linux - 无法使用 Linux 在 chrome 上执行 nightwatch 测试

java - 弹出并叠加页面

java - 在 Android 的 java 中使用 Microsoft Speech API 文本转语音?

c# - Selenium C# : How to pull value from input element

xpath - 在Selenium WebDriver中声明用于检查URL的命令

node.js - webdriver-manager 更新抛出错误 : connect ETIMEDOUT

java - 如何使用Chrome浏览器处理 "StaleElementReferenceException"