Python 使用Splinter运行测试时XPath未解析为链接

Python 使用Splinter运行测试时XPath未解析为链接,python,xpath,ui-automation,splinter,Python,Xpath,Ui Automation,Splinter,这是Splinter 0.5.4和Python 2.7.5的版本。 Firefox 22.0,在MacBook Air上运行Mountain Lion 以下是我想做的- 一,。登录到Gmail 二,。单击垃圾箱链接 三,。单击“垃圾箱”页面中的“立即清空垃圾箱”链接 四,。在确认对话框中单击确定 使用Firebug/FirePath-此XPath- `//div/div[3]/div[3]/div/span`或 `//div/div[3]/div[3]/div/span[@id]` 突出显示“立

这是Splinter 0.5.4和Python 2.7.5的版本。 Firefox 22.0,在MacBook Air上运行Mountain Lion

以下是我想做的-

一,。登录到Gmail

二,。单击垃圾箱链接

三,。单击“垃圾箱”页面中的“立即清空垃圾箱”链接

四,。在确认对话框中单击确定

使用Firebug/FirePath-此XPath- `//div/div[3]/div[3]/div/span`或 `//div/div[3]/div[3]/div/span[@id]` 突出显示“立即清空垃圾桶”链接。

但是,当我使用Splinter运行时,XPath不会解析到该链接(因此,我在调用链接上的click()方法时会得到AttributeError)

你知道为什么Splinter无法解析链接吗? 根据我的检查,XPath似乎还可以

非常感谢您的帮助

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()

我认为您的xpath不太正确。以下是对我有效的方法:

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()
不过,您应该考虑简化xpath表达式,具有绝对路径的xpath太脆弱了


希望能有所帮助。

将绝对XPath更新为
//div/div[2]/div[3]/div/span[@id]
,使其更短。