简体   繁体   English

如何将随机生成的python数学方程式发送到Chrome

[英]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. 这是做什么的:在-99999999和999999之间选择2个数字以及一个随机运算符。 It then proceeds to print it the complete equation in the cmd window. 然后,它将在cmd窗口中将其完整公式打印出来。 After it does that it opens up Chrome and goes to Google. 完成后,它将打开Chrome并转到Google。 It then types the answer of the equation (which was printed in the cmd window) 然后键入方程的答案(已在cmd窗口中打印)

What I want: I want the equation to be inputted into the Chrome window. 我想要的是:我希望方程式输入到Chrome窗口中。 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: 为我工作,我明白了:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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