需要有关Selenium元素选择的帮助

时间:2014-02-26 12:48:01

标签: html5 selenium xpath

我有以下HTML代码来创建一个复选框,但XPath和CSS选择器无法识别它。还有另一种处理方法吗?

<span class="checklist">
    <span class="">
        <input id="GenerateChargeFiles" type="checkbox" value="true" name="GenerateChargeFiles" data-val-required="The GenerateChargeFiles field is required." data-val="true">
        <input type="hidden" value="false" name="GenerateChargeFiles">
        <a class="checkbox-select">Check</a>
        <a class="checkbox-deselect">Uncheck</a>
    </span>
</span>

2 个答案:

答案 0 :(得分:0)

您尝试使用哪种XPath? XPath id('GenerateChargeFiles')应该可以正常工作。下面的示例使用Python:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("C:\\Users\\Tetrinity\\Documents\\test.html")
element = browser.find_element_by_xpath("id('GenerateChargeFiles')")
element.click()

...其中test.html包含:

<html><body>
<span class="checklist">
    <span class="">
        <input id="GenerateChargeFiles" type="checkbox" value="true" name="GenerateChargeFiles" data-val-required="The GenerateChargeFiles field is required." data-val="true">
        <input type="hidden" value="false" name="GenerateChargeFiles">
        <a class="checkbox-select">Check</a>
        <a class="checkbox-deselect">Uncheck</a>
    </span>
</span>
</body></html>

答案 1 :(得分:0)

WebElement checkBox = getDriver().findElement(By.id("GenerateChargeFiles"))