【问题标题】:selenium XPATH matches methodselenium XPATH 匹配方法
【发布时间】:2010-08-02 08:16:22
【问题描述】:

我在运行matches XPATH 方法时遇到问题:

@Test(groups = "regression")
@Parameters( { "baseUri", "locale" })
public void testShopping(String baseUri, String locale) throws Exception{
    session().allowNativeXpath("false");
    String shoppingLink = session().getAttribute("//div[@id='footernav_links']//a[matches(@href,'.*shopping.*')]/@href");
    LOGGER.info(shoppingLink);
}

抛出异常

1:25.871 INFO - Command request: getAttribute[//div[@id='footernav_links']//a[matches(@href,'.*shopping.*')]/@href, ] on session 057af825ff224b16877a61b97b974b72 13:31:25.912 INFO - Got result: ERROR: Element //div[@id='footernav_links']//a[matches(@href,'.*shopping.*')]/ not found on session 057af825ff224b16877a61b97b974b72 13:3

我正在使用 FF3.6,并从文章中获得了编写此代码的动机

how-to-use-regex-in-selenium-locators

【问题讨论】:

    标签: java regex xpath selenium


    【解决方案1】:

    您可以按如下方式使用contains

    xpath=id('footernav_links')//a[contains(@href, '/shopping/')]@href
    

    另外,最后的@ 之前的/ 不是必需的,您可以将//div[@id='footernav_links'] 简化为id('footernav_links'),如上所示。请注意,如果您这样做,您将需要 xpath= 前缀。


    或者,您可以使用 CSS 定位器:

    css=#footernav_links a[href*=/shopping/]@href
    

    【讨论】:

    • 谢谢......这种语法更紧凑,我也可以通过字符串替换重用表达式。保存了几行代码:-)
    【解决方案2】:

    让它工作。我现在使用以下语法

    session().allowNativeXpath("false");
    String shoppingLink = session().getAttribute(
        "//div[@id='footernav_links']//a[matches(@href,'/shopping/')]/@href");
    LOGGER.info(shoppingLink);
    

    我正在搜索的链接格式为
    http://mysite.com/wcs/shopping/overview?cc=gb&lc=en
    我不确定这是否完全正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多