Search code examples
pythonxpathui-automationsplinter

XPath not resolving to link when running test using Splinter


This is with Splinter 0.5.4, and Python 2.7.5. Firefox 22.0, on a MacBook Air running Mountain Lion.

Here is what I am trying to do -

1. Login to Gmail.

2. Click the Trash link.

3. Click the Empty trash now link in the Trash page.

4. Click OK in the confirmation dialog.

Using Firebug / FirePath - this XPath - `//div/div[3]/div[3]/div/span` or `//div/div[3]/div[3]/div/span[@id]` highlights the Empty trash now link.

But when I run using Splinter, the XPath does not resolve to that link, (and consequently I get an AttributeError on calling the click() method on the link).

Any idea on why Splinter is not able to resolve to the link? From what I have checked, the XPath seems to be ok.

Any help is very much appreciated.

def emptyTrash():
    browser.click_link_by_href("https://mail.google.com/mail/u/0/?shva=1#trash")
    print browser.is_element_present_by_xpath("//div/div[3]/div[3]/div/span", wait_time=5)
    deleteLink = browser.find_by_xpath("//div/div[3]/div[3]/div/span[@id]")
    print deleteLink #prints an empty list, since the above xpath is not finding the link
    deleteLink.click() #AttributeError
    trashokButton = browser.find_by_name("ok")
    trashokButton.click()

Solution

  • I think your xpath is not quite right. Here's what works for me:

    from splinter import Browser
    import time
    
    URL = 'https://mail.google.com/mail/u/0/?shva=1#trash'
    
    with Browser() as browser:
        browser.visit(URL)
    
        username = browser.find_by_id('Email')
        username.fill(...)
    
        password = browser.find_by_id('Passwd')
        password.fill(...)
    
        button = browser.find_by_id('signIn')
        button.click()
    
        time.sleep(5)
    
        browser.visit(URL)
    
        empty_button = browser.find_by_xpath("//div[5]/div[2]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div[2]/div[3]/div/span")
        empty_button.click()
    

    Though, you should think about simplifying the xpath expression, xpaths with absolute paths are too fragile.

    Hope that helps.