Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得';选择索引';属性,该属性在Python和selenium中使用JavaScript_Javascript_Python 3.x_Selenium - Fatal编程技术网

如何获得';选择索引';属性,该属性在Python和selenium中使用JavaScript

如何获得';选择索引';属性,该属性在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

我正在尝试执行一个JavaScript脚本,以从Selectwebelement获取所选选项的索引,如下所示:

@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')