使用注意:以下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’))