python - 如何在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)
解决方案
返回值中可能混合了一些 unicode 字符,您可以使用encode('ascii', 'ignore')
current_radius = int(first_radius_20percent.get_attribute('value').encode('ascii', 'ignore'))
推荐阅读
- python-3.6 - linear_model.py:1283: RuntimeWarning: sqrt return rho, np.sqrt(sigmasq) 中遇到无效值
- ruby-on-rails - 选择 1 作为一个 - Ruby 查询在 staging 和 prod 中回滚
- django - Django ModuleNotFoundError:在 Apache 中使用 mod_wsgi 没有名为“idoc”的模块
- c++ - QDoubleSpinBox 在值和显示之间进行缩放
- groovy - 我可以保持 Groovy 电源断言并且仍然打印自定义消息吗?
- java - 通用 Xml/JSON 解析器(最好是 SAX 或 STAX):根据每个不同的 xml 格式通过配置文件提供节点/xpath 信息
- react-native - 反应原生 Azure Iothub 客户端
- javascript - 如何拥有多色折线谷歌地图
- mysql - 如何从db中选择ip?
- angular - 使用 angular-azure-blob-service 时出现 403 错误