首页 > 解决方案 > 如何在python selenium中将文本框数据转换为整数

问题描述

我正在努力从文本框中获取文本,将其转换为整数,乘以 1.2,然后将其重新插入文本框中。

我最初收到一个错误,说我无法将它转换为整数(即​​使字符串刚刚说'2000'),所以我尝试将其转换为浮点数,然后转换为整数,但现在我得到'ValueError:不能将字符串转换为浮点数'。

任何想法出了什么问题?我将附上文本框的 HTML 和我的 python。

HTML:

<input id="radius" ng-model="geoCtrl.lineRadius" 
type="text" placeholder="Desired radius from each point of the list" 
maxlength="100" name="targeting[geolocation][radius]" 
ng-class="{'val-ignore': geoCtrl.options !== 'geo'}" 
class="col-lg-12 attr-input ng-pristine ng-valid">

Python:

#code to find radius, multiply it by 1.2, then enter new radius into textbox
browser.find_element_by_id('radius')
first_radius_20percent = browser.find_element_by_id('radius')
current_radius = float(first_radius_20percent.get_attribute('value'))
current_radius = int(first_radius_20percent.get_attribute('value'))
new_radius = int(current_radius*1.2)
first_radius_20percent.clear()
first_radius_20percent.send_keys(new_radius)

标签: pythonseleniumselenium-webdriverintegerselenium-chromedriver

解决方案


返回值中可能混合了一些 unicode 字符,您可以使用encode('ascii', 'ignore')

current_radius = int(first_radius_20percent.get_attribute('value').encode('ascii', 'ignore'))

推荐阅读