【问题标题】:how to convert textbox data to integer in python selenium如何在python selenium中将文本框数据转换为整数
【发布时间】:2020-01-08 11:33:14
【问题描述】:

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

我最初收到一条错误消息,说我无法将其转换为整数(即​​使字符串刚刚说“2000”),所以我尝试将其转换为浮点数并将 then 转换为整数,但是现在我收到“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)

【问题讨论】:

  • first_radius_20percent.get_attribute('value') 返回什么? '2000' 还是只在网站上看到?
  • @Guy 它返回 u'2000' 编辑:实际上当我打印 first_radius_20percent.get_attribute('value') 它只打印 '2000'

标签: python selenium selenium-webdriver integer selenium-chromedriver


【解决方案1】:

返回值中可能混入了一些unicode字符,你可以用encode('ascii', 'ignore')去掉它们

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

【讨论】:

  • 我试过这个,但现在我收到错误 ValueError: invalid literal for int() with base 10: ''
  • @booooooky 错误消息中的值是什么? ...with base 10: b'2000.0'?
  • 没有给出任何值,只是 'base 10:'' - 这是否表明没有值被提取?为什么?
  • @booooooky 你说first_radius_20percent.get_attribute('value') 返回一个值。你也可以试试value = ''.join(c for c in value if c.isprintable()),其中valueget_attribute返回的值。
  • 是的,它返回“u'2000'”(没有外引号)
【解决方案2】:

所以解决方案不是数据类型的问题,我的代码在加载后立即提取文本框的值,但仅在半秒左右后才填充“2000”。我找到的解决方案是在执行相关 python 脚本之前添加一个时间延迟。

感谢大家的帮助!

【讨论】:

    【解决方案3】:

    根据您共享的 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">
    

    我没有看到 value 例如2000placeholder 作为元素内列表中每个点的所需半径。但是,根据您的问题,因为元素是 Angular 元素,因此要提取元素的 value,您必须为 element_to_be_clickable() 诱导 WebDriverWait

    此外,由于您必须将提取的值乘以 1.2,因此最好将 get_attribute() 返回的值转换为 float,但在调用 send_keys() 时,您必须转换为string 再次使用以下解决方案:

    • 代码块:

      driver.get('https://www.google.com/')
      first_radius_20percent = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "q")))
      # current_float_radius = float(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "radius"))).get_attribute('value'))
      # or
      # current_float_radius = float(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#radius"))).get_attribute('value'))
      current_float_radius = float("2000")
      new_radius = float(current_float_radius*1.2)
      first_radius_20percent.send_keys(str(new_radius))
      

    浏览器快照:

    【讨论】:

    • 你读过这个问题吗? 我尝试将其转换为浮点数,然后转换为整数,但现在我收到“ValueError:无法将字符串转换为浮点数”
    • @booooooky 有机会评估这个答案吗?
    • @DebanjanB 现在就可以了,给我 2 秒 :)
    • @DebanjanB 就在这个上,我需要它先读取文本框,因为值不会总是 2000?
    • 查看更新的答案并让我知道状态。
    猜你喜欢
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多