Can you search html attributes using wildcards with ruby nokogiri

廉价感情. 提交于 2020-02-02 13:49:06

问题


I know you can search text in html using wildcards. Can you search for attribute values in html using wildcards with nokogiri

e.g., suppose I want to search for classes with value *session*


回答1:


You can use xpath contains() function to search the document. Something like:

doc.xpath("//*[@*[contains(., 'session')]]").each do |ele|
  # something
end

This search returns all the elements with any attribute whose value contains the string 'session'.




回答2:


Had a similar problem few days ago - notice spaces around class values.

find(:xpath, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-edit ')]")


来源:https://stackoverflow.com/questions/11593489/can-you-search-html-attributes-using-wildcards-with-ruby-nokogiri

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