selenium中的xpath用于单击元素

时间:2016-04-06 06:56:42

标签: selenium xpath

这是HTML代码,对于这个框,请告诉我如何为此框中的任何元素找到正确的xpath。 该框包含元素列表,单击时,元素从左向右传输,反之亦然。

<div id="companyIndustry" class="form-group" style="margin:0">
    <select id="my-select-industry" name="segmentdiv:companyIndustries" multiple="multiple" size="8">
        <option value="1">Agriculture and Mining - Farming and Ranching</option>
        <option value="2">Agriculture and Mining - Fishing, Hunting and Trapping</option>
    </select>
</div>

enter image description here

2 个答案:

答案 0 :(得分:0)

Xpath可以是: -

//select[@id='my-select-industry']/option[@value='2']

在这种情况下,您可以使用

Select dropdown = new Select(driver.findElement(By.id("my-select-industry")));

要选择“农业与采矿 - 捕鱼,狩猎和诱捕”,您可以选择

dropdown.selectByVisibleText("Agriculture and Mining - Fishing, Hunting and Trapping ");

dropdown.selectByIndex(2);

 dropdown.selectByValue("2");

希望它会对你有所帮助:)。

答案 1 :(得分:0)

List<WebElement> options = driver.findElements(By.xpath("//option"));

你也可以搜索&#39;选项&#39;带有相对xpath的标签(从已知的&#39; [之前找到的]元素向下搜索),但这需要你添加一个&#39;。&#39;在&#39;选项之前点而不是双斜线。

e.g。

WebElement selectElement = driver.findElement(By.id("my-select-industry"));     
List<WebElement> options = selectElement.findElements(By.xpath("./option"));

您在此处尝试做的事情的类似示例:http://forum.testproject.io/index.php?topic=19.0

相关问题