本文介绍了按索引从下拉列表中选择第一项不起作用.未绑定的方法 select_by_index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试单击下拉菜单中的第一项.

I am trying to click the first item from a drop down.

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

I want to use it's index value because the value could be different each time.

我只需要为该特定测试选择下拉列表中的第一项.

I only need to select the 1st item in the drop down for this particular test.

我试过 Select.select_by_index(1)

I have tried 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)

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

My code snippet to call the drop down is:

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

推荐答案

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

I think you need to use select instead of Select on selecting by index like below (and also i hope need to use 0 for first option in java prospective)

select.select_by_index(1) # select the 1st item from the sort drop down

在Java中一般我会这样使用

In Java generally i will use like this

  Select oSelect = new Select(driver.findElement(By.id("myDropdown")));
  oSelect.selectByIndex(0);

这篇关于按索引从下拉列表中选择第一项不起作用.未绑定的方法 select_by_index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:21