0

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!

iPhynx
  • 443
  • 6
  • 11
  • 1
    Good code. You definitely don't want to use bad code for something like this. – Mad Physicist Feb 10 '16 at 18:57
  • 1
    Look into [selenium](http://selenium-python.readthedocs.org/) – wpercy Feb 10 '16 at 18:57
  • @wilbur - Is selenium a tool/module for accessing HTML elements or something like that? – iPhynx Feb 10 '16 at 18:58
  • Not quite. It allows you to interact with a webpage like you would using normal user-input controls (ie mouse, keyboard, etc.) – wpercy Feb 10 '16 at 19:00
  • 2
    Selenium is one way to do what you are describing, but unless you are doing end to end testing (i.e. protractor), likely it's a really inefficient way of accomplishing what you are trying to do. What are you trying to do? – Gillespie Feb 10 '16 at 19:01

1 Answers1

3

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!

dot.Py
  • 5,007
  • 5
  • 31
  • 52
  • Hey there, I have created an app that goes on a profile, takes the username and tries it as a password. Everything works fine, it's just that I'm finding the username using find_element_by_css_selector, and as you might know, it returns the actual object. How would I convert this to string form? I've tried classes and __repr__ but that would be inefficient in large loops. – iPhynx Feb 10 '16 at 20:48
  • What I'm trying to do is PG old, unused accounts by using the username as the password. – iPhynx Feb 10 '16 at 20:48