【问题标题】:Lettuce Webdriver can't find my link by xpathLettuce Webdriver 通过 xpath 找不到我的链接
【发布时间】:2015-01-16 09:15:55
【问题描述】:

Lettuce 提取行为驱动的“句子”并执行用于测试的代码。

有一个“网络驱动程序”可以很好地与 selenium 集成。

我的链接是这样的:

<div class="pull-right">
    <a href="/logout">Logout ?</a>
</div>

我正在尝试使用github readme 中描述的步骤(其中一个):

I should see a link to "Google" with the url "http://google.com/"
I should see a link that contains the text "Foobar" and the url "http://foobar.com/"

与此代码对应:

@step('I should see a link to "(.*?)" with the url "(.*?)"$')
def should_see_link_text(step, link_text, link_url):
    assert_true(step,
                world.browser.find_element_by_xpath(str(
                    '//a[@href="%s"][./text()="%s"]' %
                    (link_url, link_text))))


@step('I should see a link that contains the text "(.*?)" '
      'and the url "(.*?)"$')
def should_include_link_text(step, link_text, link_url):
    return world.browser.find_element_by_xpath(str(
        '//a[@href="%s"][contains(., %s)]' %
        (link_url, link_text)))

但是当我在 Chrome 开发工具中选择我的链接时,它会显示为这个 x-path:

//*[@id="bs-example-navbar-collapse-1"]/div/div[2]/a

什么都不像:'//a[@href="%s"][contains(., %s)]''//a[@href="%s"][./text()="%s"]'

那么我应该使用哪个步骤来捕获我的链接,我是否需要更改我的 html 以匹配 lettuce-webdriver 中可用的内容(提示:这不好!)?最好不必指定 id 或类等。

编辑:LWD 给出的错误:

    Traceback (most recent call last):
      File "C:\Python34\lib\site-packages\lettuce\core.py", line 144, in __call__ret = self.function(self.step, *args, **kw)
      File "C:\Python34\lib\site-packages\lettuce_webdriver\webdriver.py", line 107, in should_include_link_text(link_url, link_text)))
      File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 230, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath)
      File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 662, in find_element {'using': by, 'value': value})['value']
      File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute self.error_handler.check_response(response)
      File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//a[@href=\"/logout\"][contains(., \"Logout ?\")]"}
    Stacktrace:
        at FirefoxDriver.prototype.findElementInternal_ (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensi
ons/fxdriver@googlecode.com/components/driver-component.js:9641:26)
        at FirefoxDriver.prototype.findElement (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensions/fxdri
ver@googlecode.com/components/driver-component.js:9650:3)
        at DelayedCommand.prototype.executeInternal_/h (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensio
ns/fxdriver@googlecode.com/components/command-processor.js:11635:16)
        at DelayedCommand.prototype.executeInternal_ (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensions
/fxdriver@googlecode.com/components/command-processor.js:11640:7)
        at DelayedCommand.prototype.execute/< (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensions/fxdriv
er@googlecode.com/components/command-processor.js:11582:5)

让我担心的是我的"selector":"//a[@href=\"/logout\"][contains(., \"Logout ?\")]" snip 的自动转义。如果它正在寻找 \"/logout\"\"Logout ?\" 的确切字符串,那么它显然是行不通的。

【问题讨论】:

    标签: python selenium xpath lettuce


    【解决方案1】:

    我会直接依赖the link text:

    world.browser.find_element_by_link_text('Logout ?')
    

    或:

    world.browser.find_element_by_partial_link_text('Logout')
    

    【讨论】:

    • 我假设在我的*step.py 文件中有一个自定义步骤?我仍然想知道为什么这不起作用....
    • @Pureferret 是的,自定义步骤。好吧,should_include_link_text 步骤至少不起作用,因为%s 周围缺少引号。
    • Alex,我刚刚用//a[@href="/logout"][contains(.,"Logout ?")] 将我的整个html 页面放入xmlme.com/XpathTool.aspx,它发现它很好。错误消息暗示它正在做一些疯狂的报价转义,这可能是问题所在。
    • 我如何保证find_element_by_link_text带回来的elem链接到\logout
    • 实际上看起来我并没有真正登录(尽管有'client.login()'),我得到的html不包含那个xpath。
    【解决方案2】:

    事实证明,登录 django 的 client 类不会影响 selenium。所以 selenium 看到的页面没有登录。

    “我已登录”步骤失败的原因是我没有登录。

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 2010-10-29
      • 1970-01-01
      • 2011-12-04
      • 2013-12-21
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多