水豚正则表达式xpath不起作用 [英] capybara regex xpath not working

查看:93
本文介绍了水豚正则表达式xpath不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下hmtl代码。 li id依次增加,我想生成正则表达式以便在xpath中使用但不起作用:

I have the following hmtl code. li id is increasing sequentially, and I want to generate regex so as to use in xpath but is not working:

irb#1(main):044:0* find(:xpath, "//li[@id = 'divPictureAndPrices_productListItem1']")
=> #<Capybara::Node::Element tag="li" path="/html/body/div[2]/div[7]/div[2]/div/div[3]/ol/li[2]">
irb#1(main):045:0>
    irb#1(main):017:0* all(:xpath, "//li[matches(@id, '^divPictureAndPrices_productListItem\d{0,2}$')]")
Selenium::WebDriver::Error::InvalidSelectorError: invalid selector: Unable to locate an element with the xpath expression //li[matches(@id, '^divPictureAndPrices_productListItemd{0,2}$')] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//li[matches(@id, '^divPictureAndPrices_productListItemd{0,2}$')]' is not a valid XPath expression.
  (Session info: chrome=48.0.2564.116)
  (Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Mac OS X 10.11.3 x86_64)
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/response.rb:34:in `initialize'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/common.rb:78:in `new'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/common.rb:78:in `create_response'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/default.rb:90:in `request'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:645:in `raw_execute'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:623:in `execute'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:602:in `find_elements_by'
    from /Library/Ruby/Gems/2.0.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/common/search_context.rb:84:in `find_elements'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/selenium/driver.rb:69:in `find_xpath'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/node/base.rb:107:in `find_xpath'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/query.rb:110:in `block in resolve_for'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/node/base.rb:80:in `synchronize'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/query.rb:106:in `resolve_for'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/node/finders.rb:182:in `block in all'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/node/base.rb:84:in `synchronize'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/node/finders.rb:181:in `all'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/session.rb:686:in `block (2 levels) in <class:Session>'
    from /Library/Ruby/Gems/2.0.0/gems/capybara-2.6.2/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
    from (irb#1):17irb#1(main):018:0>

这也不起作用:

find(:xpath, "//li[matches(@id, '^divPictureAndPrices_productListItem\d{0,2}$')]")

请帮助我解决问题吗?

推荐答案

matches仅在xpath2.0中受支持。

matches is supported only in xpath2.0.

如果您的xpath处理器仅兼容1.0,则必须使用contains。

if your xpath processor is 1.0 compliant only, then you must use contains.

这篇关于水豚正则表达式xpath不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆