how to convert textbox data to integer in python selenium

1.2k Views Asked by At

I am struggling to get the text from a textbox, convert it to an integer, multiply by 1.2 and then re-insert it back into the textbox.

I originally got an error saying I couldn't convert it to an integer (even though the string just said '2000') so I tried converting it to float and then to an integer, but now I'm getting 'ValueError: could not convert string to float'.

Any ideas what's going wrong? I'll attach the HTML of the textbox and my 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)
3

There are 3 best solutions below

8
Guy On

There might be some unicode characters mixed in the returned value, you can get rid of them with encode('ascii', 'ignore')

current_radius = int(first_radius_20percent.get_attribute('value').encode('ascii', 'ignore'))
5
undetected Selenium On

As per the HTML you have shared:

<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">

I don't see the value e.g. 2000 but a placeholder as Desired radius from each point of the list within the element. However as per your question as the element is an an Angular element so to extract the value of the element you have to induce WebDriverWait for the element_to_be_clickable().

Further, as you have to multiply the extracted value by 1.2 it would be better to convert the value returned from get_attribute() into float but while invoking send_keys() you have to convert into a string again and you use the following solution:

  • Code Block:

    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))
    

Browser Snapshot:

float

0
booooooky On

so the solution wasn't an issue with datatype, my code was pulling the value for the textbox as soon as it loaded, but the '2000' was only being populated after half a second or so. The solution I found was to just add in a time delay before carrying out the python script in question.

Thank you for all the help though!