wpm2341
先决条件
pip install selenium
程式码片段
# Importing required modules and initializing variablesfrom 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并了解技术 ,即可构建什么样的很酷的项目。
感谢您阅读到这里😏
翻译自: https://hackernoon.com/980-wpm-typing-bot-with-python-just-using-12-lines-of-code-ig2qj326c
wpm2341