Wednesday, November 30, 2011

Selenium xpath arrays

Accessing xpath array elements in Selenium can be tricky! Sometimes you may have a number of elements on a page that can only be referenced with the same locator (for example, you can't rely on unique id's, only on some field they all have in common)

example:
a website has 4 buttons with 'addToCart' in onClick being the only reliable bit to check:

  ("//button[contains(@onclick,'addToCart')])

  you might think you could click on them by subscripting like this:
  sel.click("//button[contains(@onclick,'addToCart')][1]")
  sel.click("//button[contains(@onclick,'addToCart')][2]")

but no. the first one succeeds and fakes you into thinking you can access the array this way. but you can't. if you take out the [1] you get the same result. 

So what you have to do is isolate the array first, using the 'xpath=' notation, and then subscript it:

  sel.click("xpath=(//button[contains(@onclick,'addToCart')])[3]")

notice the use of 'xpath=' and the extra parens around the locator, followed by the subscript!

Wednesday, November 9, 2011

Reading Outlook Email With Python

There are several ways to read Outlook Email with Python, and I scouted around a number of blogs and websites to piece together a method that worked for me. This may not work for you, depending on how your company sets up its Outlook, but here it is, as a Python class

here is some usage ...

  outlook = OutlookLib()
  messages = outlook.get_messages('you@yourcompany.com')
  for msg in messages:
      print msg.Subject
      print msg.Body

and here is the class ...


import win32com.client

class OutlookLib:
        
    def __init__(self, settings={}):
        self.settings = settings
        
    def get_messages(self, user, folder="Inbox", match_field="all", match="all"):      
        outlook = win32com.client.Dispatch("Outlook.Application")
        myfolder = outlook.GetNamespace("MAPI").Folders[user] 
        inbox = myfolder.Folders[folder] # Inbox
        if match_field == "all" and match =="all":
            return inbox.Items
        else:
            messages = []
            for msg in inbox.Items:
                try:
                    if match_field == "Sender":
                        if msg.SenderName.find(match) >= 0:
                            messages.append(msg)
                    elif match_field == "Subject":
                        if msg.Subject.find(match) >= 0:
                            messages.append(msg)
                    elif match_field == "Body":
                        if msg.Body.find(match) >= 0:
                            messages.append(msg)
                    #print msg.To
                    #msg.Attachments
                        # a = item.Attachments.Item(i)
                        # a.FileName
                except:
                    pass
            return messages
        
    def get_body(self, msg):
        return msg.Body
    
    def get_subject(self, msg):
        return msg.Subject
    
    def get_sender(self, msg):
        return msg.SenderName
    
    def get_recipient(self, msg):
        return msg.To
    
    def get_attachments(self, msg):
        return msg.Attachments

Friday, November 4, 2011

Selenium RC and Confirmation Dialogs

This is worth noting. Sometimes in Selenium RC you will click on a button and it will bring up a confirmation dialog (Are You Sure You Want To Do This?)

Here's how to handle it in python-selenium


sel.choose_ok_on_next_confirmation() # will 'ok' the next one that comes up
sel.click('link='Delete') # your delete button
sel.get_confirmation() # absorbs the confirmation dialog