quietlop.blogg.se

How to make a computer program interact with a web browser
How to make a computer program interact with a web browser








  1. HOW TO MAKE A COMPUTER PROGRAM INTERACT WITH A WEB BROWSER DRIVERS
  2. HOW TO MAKE A COMPUTER PROGRAM INTERACT WITH A WEB BROWSER DRIVER
  3. HOW TO MAKE A COMPUTER PROGRAM INTERACT WITH A WEB BROWSER CODE
  4. HOW TO MAKE A COMPUTER PROGRAM INTERACT WITH A WEB BROWSER PASSWORD

We again need to guide the program through the webpage by specifying exactly the elements to click on and the information to enter. Once we are logged in, we are greeted by this slightly intimidating dashboard:

HOW TO MAKE A COMPUTER PROGRAM INTERACT WITH A WEB BROWSER PASSWORD

# Find password box pass_box = driver.find_element_by_name('password') # Send password pass_box.send_keys('my_password') # Find login button login_button = driver.find_element_by_name('submit') # Click login login_button.click()

how to make a computer program interact with a web browser

Then, we send information to the elements or click on them as needed. We carry out the same process for the password box and login button, selecting each based on what we see in the Chrome developer tools. # Send id information id_box.send_keys('my_username') Our program now has access to the id_box and we can interact with it in various ways, such as typing in keys, or clicking (if we have selected a button).

how to make a computer program interact with a web browser

HOW TO MAKE A COMPUTER PROGRAM INTERACT WITH A WEB BROWSER DRIVERS

Web drivers in selenium have many different methods for selecting elements on a webpage and there are often multiple ways to select the exact same item: # Select the id box id_box = driver.find_element_by_name('username') # Equivalent Outcome! id_box = driver.find_element_by_id('username') To select the id box with our web driver, we can use either the id or name attribute we found in the developer tools. (these are known as attributes of the HTML tag). This HTML might look overwhelming, but we can ignore the majority of the information and focus on the id = "username" and name="username" parts. HTML in Chrome developer tools for the webpage When we open the Canvas webpage, we are greeted with our first obstacle, a login box! To get past this, we will need to fill in an id and a password and click the login button.

HOW TO MAKE A COMPUTER PROGRAM INTERACT WITH A WEB BROWSER DRIVER

import selenium # Using Chrome to access web driver = webdriver.Chrome() # Open the website driver.get(' ') In this case, I’ll use Chrome as my browser and send the driver to the Canvas website where I submit assignments. To get started with selenium, we import the library and create a web driver, which is a browser that is controlled by our program. The next step is to use selenium to navigate to the correct webpage and upload the assignment. This takes care of file management and the program now knows the program and the assignment to turn in. The first part of the program is a loop to go through the folders to find the assignment and class, which we store in a Python tuple: # os for file management import os # Build tuple of (class, file) to turn in submission_dir = 'completed_assignments' dir_list = list(os.listdir(submission_dir)) for directory in dir_list: file_list = list(os.listdir(os.path.join(submission_dir, directory))) if len(file_list) != 0: file_tup = (directory, file_list) print(file_tup) ('EECS491', 'Assignment 3 - Inference in Larger Graphical Models.txt') Here’s an example where the name of the class is EECS491 and the assignment is “Assignment 3 - Inference in Larger Graphical Models”.įile structure (left) and Complete Assignment (right) The program can figure out the name of the class from the folder, and the name of the assignment by the document title. In the child folders, I place the completed document named for the particular assignment. I went with a simple approach and created a folder to hold completed assignments with child folders for each class. Starting with the basics, I need a way to tell the program the name of the assignment to submit and the class. I want to write a program to submit completed course assignments to the correct location on Canvas (my university’s “learning management system”). Jumping right into programming without a plan is a great way to waste many hours in frustration. Approachīefore we can get to the fun part of automating the web, we need to figure out the general structure of our solution. (If you want to see the complete code, it’s available on GitHub). Nonetheless, the general techniques here can be applied to a limitless number of situations.

how to make a computer program interact with a web browser

HOW TO MAKE A COMPUTER PROGRAM INTERACT WITH A WEB BROWSER CODE

While this program does work (I’m using it every day!) it’s pretty custom so you won’t be able to copy and paste the code for your application. Along the way, we’ll cover the basics of using Python and selenium to programmatically control the web. Here, I’ll walk through the solution I developed to automatically (and correctly) submit my assignments. With selenium and Python, we just need to write a script once, and which then we can run it as many times and save ourselves from repeating monotonous tasks (and in my case, eliminate the chance of submitting an assignment in the wrong place)! Anytime we find ourselves repeating tedious actions on the web with the same sequence of steps, this is a great chance to write a program to automate the process for us.










How to make a computer program interact with a web browser