capybara regex xpath not working

好久不见. 提交于 2019-12-24 18:07:58

问题


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>

this is also not working:

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

please help me on solving problem?


回答1:


matches is supported only in xpath2.0.

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




回答2:


find(:xpath, "//li[contains(@id, 'divPictureAndPrices_productListItem')]")


来源:https://stackoverflow.com/questions/35792862/capybara-regex-xpath-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!