TypeError: argument of type 'int' is not iterable error using select_by_value() from dropdown dynamically using pytest parameterization

TypeError: argument of type 'int' is not iterable error using select_by_value() from dropdown dynamically using pytest parameterization

我有一个测试数据 sheet,我在其中提供了 suminsured 值,同样应该是网页上可用的 suminsured 下拉列表中的 select。以下是在 BasePage 中编写的代码,我在其中定义了 select class 来标识下拉列表

def SelectSumInsured(self,sumvalue):
    sumdropdown = Select(self.driver.find_element(By.XPATH, "//select[@formcontrolname = 'income']"))
    suminsured = sumdropdown.select_by_value(sumvalue)
    return suminsured

现在在页面对象屏幕中,我继承了 BasePage 并定义了如下函数

class HealthInsurancePage(BasePage):

    def __init__(self, driver):
        super().__init__(driver)


    def SelectSum(self,sumvalue):
        self.SelectSumInsured(sumvalue)

现在在测试脚本中,我使用了 pytest 参数化数据驱动方法,它应该 select 根据我在 excel sheet[ 中提供的数据的下拉值=15=]

class Test_RSA_Health(BaseTest):

    @pytest.mark.parametrize("pin,sumvalue", dataProvider.get_data("excelsheetname"))
    def test_RSA_Health(self,pin,sumvalue):
        home = HomePage(self.driver)
        healthinsuranepage = home.SelectHealth()
        self.VerifyPresence_PinCodeTextBox()
        healthinsuranepage.landing_page()
        healthinsuranepage.InputPin(pin)
        healthinsuranepage.SelectSum(sumvalue)

对于输入文本字段的 pin,工作正常,但是当我尝试从任何下拉列表中 select 时,出现以下错误

TypeError: argument of type 'int' is not iterable

根据要求,在下面找到完整的错误堆栈

platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\aprat\AppData\Local\Programs\Python\Python39\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.9.7', 'Platform': 'Windows-10-10.0.19042-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'allure-pytest': '2.9.45', 'ast-t
ransformer': '1.0.3', 'pytest_check': '1.0.4', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'rerunfailures': '10.2', 'server-fixtures': '1.7.0', 'shutil': '1.7.0', 'soft-a
ssertions': '0.1.2', 'xdist': '2.4.0'}, 'JAVA_HOME': 'C:\Program Files\Java\jre1.8.0_301'}
rootdir: C:\Users\aprat\PycharmProjects\Demo-Web-Automation\TestCases
plugins: allure-pytest-2.9.45, ast-transformer-1.0.3, pytest_check-1.0.4, forked-1.3.0, html-3.1.1, metadata-1.11.0, rerunfailures-10.2, server-fixtures-1.7.0, shutil-1.7.0, soft-asser
tions-0.1.2, xdist-2.4.0
collecting ... total cols are  19
total rows are  3
[[560008, 700000, 9836680067, 30, 'Apratim Chaudhuri', 'apratim.chaudhuri@riskcovr.com', 'Apratim', 'Chaudhuri', '1/2/1992', 1000000, 'AMHPC9725D', 'QA', 'GD', 'SaltLake', 180, 75, 'te
stnomineefirst', 'testnomineelast', '1/2/1992']]
[[560008, 700000, 9836680067, 30, 'Apratim Chaudhuri', 'apratim.chaudhuri@riskcovr.com', 'Apratim', 'Chaudhuri', '1/2/1992', 1000000, 'AMHPC9725D', 'QA', 'GD', 'SaltLake', 180, 75, 'te
stnomineefirst', 'testnomineelast', '1/2/1992'], [560008, 500000, 9836680067, 30, 'Apratim Chaudhuri', 'apratim.chaudhuri@riskcovr.com', 'Apratim', 'Chaudhuri', '1/2/1992', 1000000, 'A
MHPC9725D', 'QA', 'GD', 'SaltLake', 180, 75, 'testnomineefirst', 'testnomineelast', '1/2/1992']]
collected 2 items                                                                                                                                                                      

test_RSA_Health.py::Test_RSA_Health::test_RSA_Health[560008-700000-9836680067-30-Apratim Chaudhuri-apratim.chaudhuri@riskcovr.com-Apratim-Chaudhuri-1/2/1992-1000000-AMHPC9725D-QA-GD-Sa
ltLake-180-75-testnomineefirst-testnomineelast-1/2/1992]
DevTools listening on ws://127.0.0.1:55170/devtools/browser/d421fa5e-76f4-4af6-8542-d3c80ff44613
[24148:11008:0212/005823.767:ERROR:chrome_browser_main_extra_parts_metrics.cc(227)] START: ReportBluetoothAvailability(). If you don't see the END: message, this is crbug.com/1216328.
[24148:11008:0212/005823.767:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] END: ReportBluetoothAvailability()
[24148:11008:0212/005823.769:ERROR:chrome_browser_main_extra_parts_metrics.cc(235)] START: GetDefaultBrowser(). If you don't see the END: message, this is crbug.com/1216328.
[24148:9888:0212/005823.772:ERROR:device_event_log_impl.cc(214)] [00:58:23.773] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to
the system is not functioning. (0x1F)
[24148:11008:0212/005823.790:ERROR:chrome_browser_main_extra_parts_metrics.cc(239)] END: GetDefaultBrowser()
FAILED

====================================================================================== FAILURES =======================================================================================
_ Test_RSA_Health.test_RSA_Health[560008-700000-9836680067-30-Apratim Chaudhuri-apratim.chaudhuri@riskcovr.com-Apratim-Chaudhuri-1/2/1992-1000000-AMHPC9725D-QA-GD-SaltLake-180-75-testn
omineefirst-testnomineelast-1/2/1992] _

self = <TestCases.test_RSA_Health.Test_RSA_Health object at 0x0000022C0C64BBB0>, pin = 560008, sumvalue = 700000, mobileno = 9836680067, selfage = 30, fullname = 'Apratim Chaudhuri'
email = 'apratim.chaudhuri@riskcovr.com', firstname = 'Apratim', lastname = 'Chaudhuri', dob = '1/2/1992', income = 1000000, pan = 'AMHPC9725D', designation = 'QA', add1 = 'GD'
add2 = 'SaltLake', height = 180, weight = 75, nomfirstname = 'testnomineefirst', nomlastname = 'testnomineelast', nomdob = '1/2/1992'

    @pytest.mark.parametrize("pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob", dataP
rovider.get_data("rsa_health"))
    def test_RSA_Health(self,pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob):
        home = HomePage(self.driver)
        healthinsuranepage = home.SelectHealth()
        self.VerifyPresence_PinCodeTextBox()
        healthinsuranepage.landing_page()
        healthinsuranepage.InputPin(pin)
>       healthinsuranepage.SelectSum(sumvalue)

test_RSA_Health.py:17:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\Pages\HealthInsurancePage.py:24: in SelectSum
    dropdown.select_by_value(sumvalue)
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.py:78: in select_by_value
    css = "option[value =%s]" % self._escapeString(value)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <selenium.webdriver.support.select.Select object at 0x0000022C0C679CD0>, value = 700000

    def _escapeString(self, value):
>       if '"' in value and "'" in value:
E       TypeError: argument of type 'int' is not iterable

..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.py:219: TypeError
---------------------------------------------------------------------------------- Captured log call ----------------------------------------------------------------------------------
INFO     Pages.BasePage:BasePage.py:27 Clicking on an Element health_XPATH
INFO     Pages.BasePage:BasePage.py:60 Typing in an Element pin_XPATH entered the value as : 560008
=============================================================================== short test summary info ===============================================================================
F

这个错误信息...

    css = "option[value =%s]" % self._escapeString(value)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <selenium.webdriver.support.select.Select object at 0x0000022C0C679CD0>, value = 700000

    def _escapeString(self, value):
>       if '"' in value and "'" in value:
E       TypeError: argument of type 'int' is not iterable

...意味着 select_by_value(value) 期望 value 为字符串格式。因为,在您的用例中 sumvalue 是一个整数。


解决方案

sumvalue转换为str,同时作为参数传递给SelectSum(),如下所示:

healthinsuranepage.SelectSum(str(sumvalue))