先决条件
pip install selenium
代码段
# Importing required modules and initializing variables
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import re
string = ''
# ______________________________________
# Opening thetypingcat.com on firefox
firefox = webdriver.Firefox()
firefox.get( 'https://thetypingcat.com/typing-speed-test/1m' )
# ______________________________________
# Using javascript to get the typing content from the website and storing value in "string" variable
for i in range(firefox.execute_script( 'return document.querySelectorAll(".line").length' )):
string += firefox.execute_script( 'return document.querySelectorAll(".line")[' +str(i)+ '].innerHTML' )
string = re.sub(r '<[^>]*>' , '' ,string) #This line is just delete tags present inside string
# ______________________________________
# Selenium commands to type what is stored inside string variable on the focused screen
action = ActionChains(firefox)
action.send_keys(string)
action.perform()
# ______________________________________ END ______________________________________
就这样
Selenium很好,但是用它构建一个很棒的项目比真棒。 现在您已经知道,仅需一点点Google搜索,查看DOM并了解技术 ,便可以构建什么样的很酷的项目。
感谢您阅读到这里till
From: https://hackernoon.com/980-wpm-typing-bot-with-python-just-using-12-lines-of-code-ig2qj326c