0

I'm trying to get the result of an input from: https://web2.0calc.com/

But I can't get the result. I've tried:

result = browser.find_element_by_id("input")

result.text

result.get_attribute("textContent")

result.get_attribute("innerHtml")

result.get_attribute("textContent")

But it doesn't work and returns an empty string...

4
  • 1
    The item on that page with id="result" is an image. Images don't have any of those attributes. Commented Dec 15, 2018 at 19:01
  • Seems like a POST request is also made if you can emulate that. Commented Dec 15, 2018 at 19:14
  • you right i meant to put id="input" which is not an image Commented Dec 15, 2018 at 20:06
  • @kobyt, check updated answer Commented Dec 15, 2018 at 20:38

2 Answers 2

2

The required element is a Base64 image, so you can either get a Base64 value from @src, convert it to an image and get a value with a tool like PIL (quite complicated approach) or you can get a result with a direct API call:

import requests

url = 'https://web2.0calc.com/calc'
data = data={'in[]': '45*23'}  # Pass your expression as a value

response = requests.post(url, data=data).json()
print(response['results'][0]['out'])
#  1035

If you need the value of #input:

print(browser.find_element_by_id('input').get_attribute('value'))
Sign up to request clarification or add additional context in comments.

1 Comment

print(browser.find_element_by_id('input').get_attribute('value')) works! thanks!
1

My preference would be for the POST example (+ for that) given but you can grab the expression and evaluate that using asteval. There may be limitations on asteval. It is safer than eval.

from selenium import webdriver
from asteval import Interpreter

d = webdriver.Chrome()
url = 'https://web2.0calc.com/'
d.get(url)
d.maximize_window()
d.find_element_by_css_selector('[name=cookies]').click()
d.find_element_by_id('input').send_keys(5)
d.find_element_by_id('BtnPlus').click()
d.find_element_by_id('input').send_keys(50)
d.find_element_by_id('BtnCalc').click()
expression = ''

while len(expression) == 0:
    expression = d.find_element_by_id('result').get_attribute('title')

aeval = Interpreter()
print(aeval(expression))
d.quit()

1 Comment

Isn't there a risk of a thrown exception from one of the find_element_by_id() or find_element_by_css_selector() calls? Is there automatic synchronisation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.