【问题标题】:XPath not resolving to link when running test using Splinter使用 Splinter 运行测试时 XPath 无法解析链接
【发布时间】:2013-07-28 12:52:11
【问题描述】:

这适用于 Splinter 0.5.4 和 Python 2.7.5。 Firefox 22.0,在运行 Mountain Lion 的 MacBook Air 上。

这是我想要做的 -

1。登录到 Gmail。

2。点击回收站链接。

3。点击回收站页面中的立即清空回收站链接。

4。在确认对话框中单击“确定”。

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

【问题讨论】:

    标签: python xpath ui-automation splinter


    【解决方案1】:

    我认为您的 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],使其更短。
    猜你喜欢
    • 1970-01-01
    • 2019-02-20
    • 2018-03-12
    • 2021-04-27
    • 2012-04-07
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多