0
点赞
收藏
分享

微信扫一扫

selenium通过js操作select选择框获取options长度,并通过js获取options中的text

f12b11374cba 2022-04-14 阅读 58
selenium

使用注意:以下js代码不支持ie 8 9 ,ie11未测试。在chrome中测试正常
背景:使用js方法获取页面select中数据无需切iframe,尤其对内网页面包含多层iframe,有非常大的帮助。

前端select代码

<select id=“person”>
	<option value=“0001”>张三</option>
	<option value=“0002”>李四</option>
</select>

Js代码获取options的长度为2:

Person = driver.execute_script(return document.getElementById(“person”).options.length’)

js代码获取每个option的text

for i in range(person):
    Print(driver.execute_script(return document.getElementById(“person”).options[‘’’ + str(i) + ‘’’].text’))
举报

相关推荐

0 条评论