Python & Selenium

Step 1:

Install Selenium libraries with Python.  If you are using Python3, then you would use:

pip3 install selenium

If using Pre-Python3:

pip install selenium

Step 2:

In your Python script, import the libraries you need… like:

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

Step 3:

Write the selenium calls as needed:

driver = web driver.Firefox()
driver.get("http://www.google.com")
assert "Google" in driver.title
field = driver.find_element_by_name("q")
field.send_keys("selenium")
field.send_keys(Keys.RETURN)
driver.close()

That should get you going.  I just wrote a modified version of the selenium python walk through.  I’ve played with it.  It runs at about the same speed as Ruby with Watir.  Watir is a better wrapper on selenium IMO. So the gain here isn’t much, if anything.

Why post it?  Well someone out there might have a hankering for using Python.  In fact I knew a manager at TrueCar that was thrilled to use Python in their web automation.  Python does have some good math/science libraries.  But if you’re not making use of specialized libraries, I’m not sure where the gain with this is.

Watir can do some interesting things like:

driver.text_field(:name=>”q”).set “selenium”
driver.send_keys :enter

Same idea, just seems to be less verbose.

Since Watir is a wrapper on Selenium Webdriver, there’s really the whole selenium functionality under the hood.

But if you want to do something with Python… well there you go.  Just use the above calls and directly access selenium’s methods/api.

Leave a Reply

Your email address will not be published. Required fields are marked *