使用`selenium-webdriver-webelement.send_keys`遇到的问题

本文介绍了一种在使用selenium-webdriver进行自动化测试时遇到的问题及解决方案。当从pandas DataFrame中读取numpy.int64类型的数字并尝试将其作为字符串输入到网页表单时,会引发TypeError异常。文章提供了修改代码的具体步骤来规避此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用selenium-webdriver-webelement.send_keys遇到的问题

读取一个网页输入框,从 pandas 读取的 DataFrame 中取出一串数字输入进去。code:

for i in DF.iloc[:,11]:
    ele = browser.find_element_by_xpath("div[1]/div[2]/div/input")
    ele.clear()
    ele.send_keys(i)

遇到如下问题:

  File "d:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 351, in send_keys
    {'text': "".join(keys_to_typing(value)),

  File "d:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\common\utils.py", line 150, in keys_to_typing
    for i in range(len(val)):

TypeError: object of type 'numpy.int64' has no len()

因为 pandas 是以 numpy 为基础的,从DF读取的一串数字自然是numpy.int64类型的。因此将ele.send_keys(i)改为ele.send_keys(int(i))即可。

File "D:\Hy_autotests\Hy_autotest\cases\Supperapp\Amap\Super_Amap_001.py", line 57, in teststeps self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("输入工具名称进行查找")').send_keys('离线地图') File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\appium\webdriver\webelement.py", line 218, in send_keys self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT, {'text': ''.join(keys), 'value': keys}) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 396, in _execute return self._parent.execute(command, params) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute self.error_handler.check_response(response) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\appium\webdriver\errorhandler.py", line 30, in check_response raise wde File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\appium\webdriver\errorhandler.py", line 26, in check_response super().check_response(response) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidElementStateException: Message: Cannot set the element to '离线地图'. Did you interact with the correct element? Stacktrace: io.appium.uiautomator2.common.exceptions.InvalidElementStateException: Cannot set the element to '离线地图'. Did you interact with the correct element? at io.appium.uiautomator2.handler.SendKeysToElement.setText(SendKeysToElement.java:87)
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值