如何在div下找到项目列表

时间:2015-09-03 08:27:12

标签: java selenium selenium-webdriver

我有以下代码:

[![



<div id="s2id_location" class="select2-container location req" style="width: 550px;">
<a class="select2-choice" tabindex="-1" href="javascript:void(0)">
<span id="select2-chosen-1" class="select2-chosen">Current location</span>
<abbr class="select2-search-choice-close"/>
<span class="select2-arrow" role="presentation">
<b role="presentation"/>
</span>
</a>
<label class="select2-offscreen" for="s2id_autogen1"/>
<input id="s2id_autogen1" class="select2-focusser select2-offscreen" type="text" role="button" aria-haspopup="true" aria-labelledby="select2-chosen-1" tabindex="0"/>
</div>
<select id="location" name="currentloc" tabindex="-1" title="" style="display: none;">
<option value="">Current location</option>
<option value="1">Ahmedabad</option>
<option value="2">Bengaluru / Bangalore</option>
<option value="3">Chennai</option>
<option value="4">Delhi</option>
<option value="5">Gurgaon</option>
&#13;
&#13;
&#13;

] 1] 1

如何从代码上方访问古尔冈。

2 个答案:

答案 0 :(得分:0)

使用像这样的xpath

//select[@name='currentloc']/option[@value='5']

OR

//select[@name='currentloc']/option[contains(text(),'Gurgaon')]

从上面选择任何一个并输入以下代码

String loc = driver.findElement(By.xpath("//select[@name='currentloc']/option[@value='5']")).gettext();
system.out.print(loc);

尝试以上两个xpaths

答案 1 :(得分:-1)

String elementval = driver.findElement(By.id("location")).getAttribute("value");