我需要从下拉列表中获取当前选中的选项并将其打印出来,但是当我尝试使用getText()时,我会在下拉菜单中获得完整的项目列表,如何获取当前项目从下拉列表中选择的项目?
感谢。 HTML代码是 -
<select id="j_id0:j_id10:main_page_block:j_id45:0:j_id46:j_id47:3:j_id48" name="j_id0:j_id10:main_page_block:j_id45:0:j_id46:j_id47:3:j_id48">
<option value="">--None--</option>
<option value="Request">Request</option>
<option value="In Authoring" selected="selected">In Authoring</option>
<option value="In Signatures">In Signatures</option>
<option value="In Filing">In Filing</option>
<option value="In Effect">In Effect</option>
<option value="Expired">Expired</option>
<option value="Terminated">Terminated</option>
<option value="Amended">Amended</option>
<option value="Cancelled">Cancelled</option>
</select>
我试过这个,但它只列出了所有选项:
driver.findElement(By.id('j_id0:j_id10:main_page_block:j_id45:0:j_id46:j_id47:3:j_id48')).getText()
答案 0 :(得分:1)
试试这个,
如果您在网页上有一个下拉列表,
driver.findElement(By.xpath("//option[@selected='selected']")).getText();
如果您在网页上有多个下拉列表,
driver.findElement(By.xpath("//select[@id='j_id0:j_id10:main_page_block:j_id45:0:j_id46:j_id47:3:j_id48']//option[@selected='selected']")).getText();