按索引从下拉列表中选择第一项不起作用。未绑定的方法 select_by_index

Ria*_*ani 7 python selenium python-2.7

我正在尝试从下拉列表中单击第一个项目。

我想使用它的索引值,因为该值每次都可能不同。

对于这个特定的测试,我只需要在下拉列表中选择第一个项目。

我试过 Select.select_by_index(1)

我收到错误:

    Traceback (most recent call last):
  File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore - Regression Test\TestCases\DataPreviewsPage_TestCase.py", line 398, in test_a2_sort_data_preview_advanced
    data_previews_view_page.select_option_from_new_sort_drop_down() # Select the sort from the sort drop down to view the sorted fields
  File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore - Regression Test\Pages\data_previews_view.py", line 144, in select_option_from_new_sort_drop_down
    Select.select_by_index(1) # select the 1st item from the sort drop down
TypeError: unbound method select_by_index() must be called with Select instance as first argument (got int instance instead)
Run Code Online (Sandbox Code Playgroud)

我调用下拉菜单的代码片段是:

def select_option_from_new_sort_drop_down(self): # When sort is ready, select the 1st value from the drop to run the sort
    select = Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//option[contains(., "(A-Z)")]'))))
    Select.select_by_index(1) # select the 1st item from the sort drop down
Run Code Online (Sandbox Code Playgroud)

Ped*_*ito 8

对于python使用:

from selenium.webdriver.support.select import Select
my_select = Select( driver.find_element_by_id("some_id") )
my_select.select_by_index(1)
Run Code Online (Sandbox Code Playgroud)


小智 5

我认为你需要使用 select 而不是 Select 来按索引选择,如下所示(而且我希望需要使用 0 作为 java 中的第一个选项)

select.select_by_index(1) # select the 1st item from the sort drop down
Run Code Online (Sandbox Code Playgroud)

在Java中我一般会这样使用

  Select oSelect = new Select(driver.findElement(By.id("myDropdown")));
  oSelect.selectByIndex(0);
Run Code Online (Sandbox Code Playgroud)