2016-03-04 127 views
1

我有以下hmtl代码。李ID依次增加,我想生成正则表达式,从而XPath中使用,但不工作:capybara正则表达式xpath不工作

enter image description here

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}$')]") 

请帮助我在解决问题?

+0

你为什么不用'contains'来试试? find(:xpath,“// li [contains(@id,'divPictureAndPrices_productListItem')]”)' – fabersky

+0

试图使用正则表达式,它给了更多的灵活性。 –

+0

@MesutGüneş在错误消息中提到的XPath表达式确实无效,但是您提到的最后一个有有效的语法,并且在测试时没有得到任何抱怨[http://www.xpathtester.com/xpath]启用XPath 2.0选项 – har07

回答

0

匹配仅在xpath2.0中受支持。

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

+0

我有以下gem'xpath(2.0.0)',是匹配来自xpath2.0。 –

+0

@MesutGüneşxpath gem没有提供XPath,它提供了用于构建xpath查询的ruby DSL。支持的XPath查询版本基于正在使用的浏览器,并且IIRC不支持XPath 2.0 –

+0

@TWWalpole我正在使用最新版本的chromedriver。 –

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

问题是在xpath中使用正则表达式。 –