简体   繁体   中英

How to send a randomly generated python math equation to Chrome

The code I am using:

import operator
import math
import random
from random import randint
from selenium import webdriver

vari1 = (random.randint(-99999999,999999))
vari2 = (random.randint(-99999999,999999))


ops = {'+':operator.add,
       '-':operator.sub,
       '*':operator.mul,
       '/':operator.truediv}

op = random.choice(list(ops.keys()))
answer = ops.get(op)(vari1,vari2)
print('{} {} {}?\n'.format(vari1, op, vari2))


browser = webdriver.Chrome()
browser.get("https://www.google.com/")
search = browser.find_element_by_name('q')
search.send_keys(answer)

What this does: Chooses 2 numbers between -99999999 and 999999 along with a random operator. It then proceeds to print it the complete equation in the cmd window. After it does that it opens up Chrome and goes to Google. It then types the answer of the equation (which was printed in the cmd window)

What I want: I want the equation to be inputted into the Chrome window. I don't want the answer but the equation

Pre-format your equation and send it with a newline at the end:

equation = '{} {} {} = \n'.format(vari1, op, vari2) 
search.send_keys(equation)

Works for me, I get this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM