Selenium WebDriver Java - 按标签单击元素不能在某些标签上工作

时间:2014-05-16 21:04:23

标签: java selenium selenium-webdriver webdriver

我试图通过标签点击元素。这是我正在使用的代码:

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

适用于(全选)&海沃德却无法找到洛杉矶,圣弗兰或圣何塞。

更新:

现在我觉得这可能是我最好的选择,直到我看到更好的东西。这将允许用户传递完整的String,并且方法中的函数将获取字符串的最后一个单词并将其插入到包含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>

8 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

使用以下xpath:

//label[text()='(Select All)']
//label[text()='Hayward']
//label[text()='Los Angeles']
//label[text()='San Francisco']
//label[text()='San Jose']

答案 2 :(得分:0)

你也可以做这样的包含:

"//label[contains(text(), 'TEXT_TO_FIND')]"

答案 3 :(得分:0)

尝试使用id而不是xpath:

//for Select All
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl00")).click();
//for Hayward
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl02")).click();
//for Los Angeles
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl03")).click();
//for San Francisco
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl04")).click();
//for San Jose
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl05")).click();

如果仍有问题或ID正在动态变化,您可以使用xpath,如:

//label[contains(.,'(Select All)')]
//label[contains(.,'Hayward')]
//label[contains(.,'Los Angeles')]
//label[contains(.,'San Francisco')]
//label[contains(.,'San Jose')]

答案 4 :(得分:0)

driver.findElement(By.xpath("//label[text()='LABEL TEXT']/../input")).click();

答案 5 :(得分:0)

尝试CSS:
driver.findElement(By.CSS("label:contains('Partial or full text')");

答案 6 :(得分:0)

试试这个,也许是一个好的起点:

System.out.print(driver.findElement(By.xpath("//div[@id='ReportViewer1_ctl04_ctl03_divDropDown']/span/table/tbody/tr[" + i +"]/td/span/label")).getText());
driver.findElement(By.xpath("//div[@id='ReportViewer1_ctl04_ctl03_divDropDown']/span/table/tbody/tr[" + i +"]/td/span/label")).click();

i = 3; //Los Angeles
i = 4; //San Francisco

我希望,我的xpath是正确的。

答案 7 :(得分:0)

使用以下代码点击标签或选择标签

driver.findElement(By.xpath("//label[@for='your id which you want to click']")).click();