How would I be able to create a bot in python that inputs text into a webpage [closed]

醉酒当歌 提交于 2021-02-07 04:38:11

问题


What code would I use to input text into a box (not edit text, something along the lines of a message bot)?

Say I run the code, it opens a webpage and inputs text into a (e.g.) search box.

Thanks!


回答1:


You can use the Selenium library to create a bot in Python that inputs texts into a webpage.

According to their description, "the selenium package is used automate web browser interaction from Python."

You can install it by typing in the terminal:

pip install selenium

Then you'll have to build a set of instructions to tell the program what you want to do.

Example:

TASKS:

1. open a new Firefox browser
2. load the Yahoo homepage
3. search for “seleniumhq”
4. close the browser

CODE:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()

browser.get('http://www.yahoo.com')
assert 'Yahoo' in browser.title

elem = browser.find_element_by_name('p')  # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)

browser.quit()

Also, read the Selenium Docs and take a look at a few videos to understand how can you build your own web bot!



来源:https://stackoverflow.com/questions/35323605/how-would-i-be-able-to-create-a-bot-in-python-that-inputs-text-into-a-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!