WebDriver and Select Boxes
This one had me puzzled for a while as I never took the time to sit down and read the documentation fully... I decided to look at this again after seeing the issue appear on the WebDriver mailing list. How to you use select and option html elements? Below is Python demo.
$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from webdriver_firefox.webdriver import FirefoxLauncher
>>> from webdriver_firefox.webdriver import WebDriver
>>> d = WebDriver()
>>> d.get("http://cassandra.appspot.com/")
>>> e = d.find_elements_by_xpath(
"/html/body/div[@id='container']/div[@id='search']/form[@id='searchForm']/div/select")
>>> r = e[0]
>>> t = r.find_elements_by_tag_name("option")
>>> t
[<webdriver_firefox.webelement.WebElement object at 0x8dd778c>,
<webdriver_firefox.webelement.WebElement object at 0x8dd772c>,
<webdriver_firefox.webelement.WebElement object at 0x8dd77ac>,
<webdriver_firefox.webelement.WebElement object at 0x8dd77ec>]
>>> for i in t:
... print i.get_text()
...
Artist
Location
last.fm Username
Venue
>>> t[2].set_selected()
>>>
Now in your WebDriver browser session the option box has changed to "last.fm Username". Excuse the variable names but I wanted to make a note before I lost the code.
Speeding up the Web
Might be old news and to be honest it should be if you do web development Speed. This page from Google contains lots of tips for speeding up web pages, lots of useful tips and worth at least one read.
Little Script to Check Twitter’s Status
#!/usr/bin/env python
from lxml import html
print "Starting..."
page = html.parse("http://www.pingdom.com/reports/vb1395a6sww3/check_overview/?name=twitter.com%2Fhome")
if page:
try:
t = page.xpath("/html/body/div[@id='content']/table[1]/tr[2]/td")
status_icon = t[0].xpath("img/@src")[0]
status = status_icon.split('_')[2].split('.')[0]
date = t[1].text
print "Twitter is %s! Checked at: %s Pacific Time (GMT - 8:00)" % (status, date)
except:
print "Unable to correctly verify Twitters status"
else:
print "Can't access http://www.pingdom.com status page!"
Conduit from SVN
Installed in it from svn and kept getting: ImportError: No module named conduit
Fixed it with a: sudo cp -r /usr/lib/python2.6/site-packages/conduit /usr/lib/python2.6/ after failing with ./autogen.sh --prefix /usr
Get CouchDB up and running
run:
sudo apt-get install libcurl4-gnutls-dev libmozjs-dev libicu-dev erlang
This should be all you require to build and install apache-couchdb-0.9.0
Playing a DVD
A friend was getting the following error when playing some dvds:
[ 172.864397] Buffer I/O error on device sr0, logical block 278
Following https://help.ubuntu.com/community/RestrictedFormats/PlayingDVDs solved it.
Google Contact API
This is more for me as a reminder.
To find the postal address from a gdata.contacts.ContactEntry use:
import atom import gdata.contacts import gdata.contacts.service gd_client = gdata.contacts.service.ContactsService() gd_client.email = 'jo@gmail.com' gd_client.password = 'passdword' gd_client.source = 'exampleCo-exampleApp-1' gd_client.ProgrammaticLogin() from lxml import etree feed = gd_client.GetContactsFeed() entry = feed.entry[0] entry entry.postal_address[0].text '123 Fake Street'
Took me ages to figure that out...
A Quick memcache demo for Python
So you need to use memcache with Python? Below is a brief intro I figured out in about 20 mins.
First install everything you need. I run Ubuntu so python-memcached memcached packages were required.
Next start the memcached server if its not already running:
/usr/bin/memcached -m 64 -p 11211 -u nobody -l 127.0.0.1
The above gets you a 64Mb server, more than enough to play on.
Next some python below is from the interpreter as I was in a hurry:
import memcache
memc = memcache.Client(['127.0.0.1:11211'])
class test():
def __init__(self):
self.m = "Hello, world"
t = test()
memc.set('cheese', t, 120)
True
r = memc.get('cheese')
r.m
'Hello, world'
The above instantiates an object and then saves it memcached with set() and then we get it back using get(). Dead simple. The usage is pretty, try and fetch from memcached if if fails fetch from your datasource and then save that document ready for next time.
memc.set('cheese', t, 120) cheese is the reference, t is the object to store and 120 is the time to live. After 120 seconds the object is cleared from the cache.
WebDriver for logging into Twitter
No real reason for choosing Twitter apart from its cool
.
The code below uses unittest to run. It creates a new WebDriver object and users it to fetch http://twitter.com and submit a username and password. Once the details are added it "clicks" the "Sign In" button to login into Twitter.
#!/usr/bin/env python
import unittest
import logging
from webdriver_firefox.webdriver import FirefoxLauncher
from webdriver_firefox.webdriver import WebDriver
class TwitterTests (unittest.TestCase):
def test_login_twitter(self):
driver = WebDriver()
driver.get("http://twitter.com")
# find our elements - the html on the page with same ids
username_element = driver.find_element_by_id('username')
password_element = driver.find_element_by_id('password')
# use this to toggle the remember me box
remember_me_element = driver.find_element_by_id('remember')
# type into the boxes
username_element.send_keys('yourusername')
password_element.send_keys('yourpassword')
remember_me_element.toggle()
# click Sign In and we should be logged in
driver.find_element_by_id('signin_submit').click()
# check that the title of the page is correct to see if we logged in
self.assertEqual(driver.get_title(), 'Twitter / Home')
# Extract from the html using xpath to find username and updates of the people on the screen
updates = driver.find_elements_by_xpath("//span[@class='entry-content']");
user = driver.find_elements_by_xpath("//a[@class='screen-name']");
# display in the terminal the name and the update
for i,update in enumerate(updates):
print user[i].get_text() + ": " +update.get_text()
# uncomment the following to close the window and finish
#driver.quit()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
unittest.main()
The docs are not great for WebDriver but reading the source is pretty simple. Being able to mentally parse Java to Python is also a big advantage!
Webdriver and Python Bindings
I`m working on some WebDriver stuff and below is quick guide to get you started:
svn checkout http://webdriver.googlecode.com/svn/trunk/ webdriver cd webdriver/ sudo python setup.py install
add the following to make sure you new libs can be found correctly
vi ~/.bashrc export WEBDRIVER=/home/channam/Code/python/webdriver . ~/.bashrc
and you are good to go.