2016-04-06 142 views
0

这是HTML代码,对于这个盒子,请教我如何找到这个盒子中任何元素的正确xpath。 该框包含元素列表,元素从左向右传递,反之亦然。xpath in selenium点击元素


<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

+0

快,让你尝试过什么到目前为止 – har07

回答

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"); 

希望它会帮助你:)

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

You co uld也用相对xpath搜索'option'标签(从'已知'[先前找到]元素向下搜索),但是这会要求你添加'。'。 '选项'之前,而不是双斜线。

例如

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