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

问题描述

我正在尝试从下拉列表中单击第一项.我想使用它的索引值,因为该值可能每次都不同.对于此特定测试,我只需要在下拉列表中选择第一项即可.我已经尝试过Select.select_by_index(1)

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.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

我正在Python中使用Selenium

I am using Selenium in Python

谢谢,里亚兹

推荐答案

我认为您需要使用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);

谢谢,穆拉利

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

11-01 03:22