如何获得';选择索引';属性,该属性在Python和selenium中使用JavaScript
我正在尝试执行一个JavaScript脚本,以从Selectwebelement获取所选选项的索引,如下所示:如何获得';选择索引';属性,该属性在Python和selenium中使用JavaScript,javascript,python-3.x,selenium,Javascript,Python 3.x,Selenium,我正在尝试执行一个JavaScript脚本,以从Selectwebelement获取所选选项的索引,如下所示: @property def selected_option_index(self): index = driver.execute_script('arguments[0].selectedIndex', self.select_field) return index 但结果是无 当我尝试更直接的方法时也会发生同样的情况,例如: index = driver.exec
@property
def selected_option_index(self):
index = driver.execute_script('arguments[0].selectedIndex', self.select_field)
return index
但结果是无
当我尝试更直接的方法时也会发生同样的情况,例如:
index = driver.execute_script('document.querySelector(\'select[name="vehicleType"]\').selectedIndex')
但是,当我尝试运行以下JavaScript时:
typeSelectIndex = document.querySelector('select[name="vehicleType"]').selectedIndex
在开发人员工具-控制台中,结果正常:
15
Python代码应该如何构造以获得所需的结果?javascript中缺少return关键字。请尝试以下方法
@property
def selected_option_index(self):
index = driver.execute_script('return arguments[0].selectedIndex', self.select_field)
return index
或
index = driver.execute_script('return document.querySelector(\'select[name="vehicleType"]\').selectedIndex')