Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling Browser using Python?

Is it possible to control a web browser like Firefox using Python?

I would want to do things like

  • launch the browser
  • force clicks on URLs
  • take screenshots

etc.

like image 577
demos Avatar asked Jul 30 '10 06:07

demos


People also ask

How do I use Python to control my browser?

To display web based documents to users by using python, there is a module called webbrowser. It provides high level interface to handle web documents. On UNIX based system, this module supports lynx, Netscape, Mosaic etc browsers. For Windows and Macintosh, it uses the standard browsers.

Can we automate web application using Python?

You have learned that Python can do everything that a web browser can do, and a bit more. You could easily write scripts to control virtual browser instances that run in the cloud. You could create bots that interact with real users or mindlessly fill out forms! Go forth and automate!

Can Python work with browsers?

There are several benefits of running Python in the browser. It allows you to: Execute the same Python code in the server and the browser. Work with various browser APIs using Python.

Which Python library is used for browser automation?

Splinter Splinter is a fantastic wrapper-type library that is specially designed for Selenium in Python. Splinter is a free open source tool that can be used to test web applications with Python. You can use this tool for automating the browser operations, such as interacting with other items, visiting URLs, etc.


1 Answers

Selenium Remote Control is a project that comes very close to what you are after. It is really easy to get working in Python with the selenium.webdriver subpackage that comes with it. Once upon a time, these were two projects. They've now been unified.

Installation

Simple!

$ pip install -U selenium 

Usage

>>> from selenium import webdriver >>> ff = webdriver.Firefox() >>> ff.get("http://stackoverflow.com/q/3369073/395287") >>> ff.save_screenshot("/absolute/path/to/webpage.png") 

Notes

The documentation can be slightly confusing for Selenium, because there are two modes to interact with browsers. As well as the webdriver mode, there is the ability to talk to a "standalone Selenium Remote Control server". That approach is what is documented first in the official documentation, but I would stick with webdriver for the simple task here.

like image 173
Tim McNamara Avatar answered Oct 06 '22 12:10

Tim McNamara